Skip to content

Commit 9bceaec

Browse files
committed
feat: add solutions to lc problem: No.1769
No.1769.Minimum Number of Operations to Move All Balls to Each Box
1 parent 7b80ae1 commit 9bceaec

File tree

5 files changed

+385
-0
lines changed

5 files changed

+385
-0
lines changed

solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,166 @@ func minOperations(boxes string) []int {
247247
}
248248
```
249249

250+
### **TypeScript**
251+
252+
```ts
253+
function minOperations(boxes: string): number[] {
254+
const n = boxes.length;
255+
const left = new Array(n).fill(0);
256+
const right = new Array(n).fill(0);
257+
for (let i = 1, count = 0; i < n; i++) {
258+
if (boxes[i - 1] == '1') {
259+
count++;
260+
}
261+
left[i] = left[i - 1] + count;
262+
}
263+
for (let i = n - 2, count = 0; i >= 0; i--) {
264+
if (boxes[i + 1] == '1') {
265+
count++;
266+
}
267+
right[i] = right[i + 1] + count;
268+
}
269+
return left.map((v, i) => v + right[i]);
270+
}
271+
```
272+
273+
```ts
274+
function minOperations(boxes: string): number[] {
275+
const n = boxes.length;
276+
const ans = new Array(n).fill(0);
277+
for (let i = 1, count = 0; i < n; i++) {
278+
if (boxes[i - 1] === '1') {
279+
count++;
280+
}
281+
ans[i] = ans[i - 1] + count;
282+
}
283+
for (let i = n - 2, count = 0, sum = 0; i >= 0; i--) {
284+
if (boxes[i + 1] === '1') {
285+
count++;
286+
}
287+
sum += count;
288+
ans[i] += sum;
289+
}
290+
return ans;
291+
}
292+
```
293+
294+
### **Rust**
295+
296+
```rust
297+
impl Solution {
298+
pub fn min_operations(boxes: String) -> Vec<i32> {
299+
let s = boxes.as_bytes();
300+
let n = s.len();
301+
let mut left = vec![0; n];
302+
let mut right = vec![0; n];
303+
let mut count = 0;
304+
for i in 1..n {
305+
if s[i - 1] == b'1' {
306+
count += 1;
307+
}
308+
left[i] = left[i - 1] + count;
309+
}
310+
count = 0;
311+
for i in (0..n - 1).rev() {
312+
if s[i + 1] == b'1' {
313+
count += 1;
314+
}
315+
right[i] = right[i + 1] + count;
316+
}
317+
(0..n).into_iter().map(|i| left[i] + right[i]).collect()
318+
}
319+
}
320+
```
321+
322+
```rust
323+
impl Solution {
324+
pub fn min_operations(boxes: String) -> Vec<i32> {
325+
let s = boxes.as_bytes();
326+
let n = s.len();
327+
let mut ans = vec![0; n];
328+
let mut count = 0;
329+
for i in 1..n {
330+
if s[i - 1] == b'1' {
331+
count += 1;
332+
}
333+
ans[i] = ans[i - 1] + count;
334+
}
335+
let mut sum = 0;
336+
count = 0;
337+
for i in (0..n - 1).rev() {
338+
if s[i + 1] == b'1' {
339+
count += 1;
340+
}
341+
sum += count;
342+
ans[i] += sum;
343+
}
344+
ans
345+
}
346+
}
347+
```
348+
349+
### **C**
350+
351+
```c
352+
/**
353+
* Note: The returned array must be malloced, assume caller calls free().
354+
*/
355+
int *minOperations(char *boxes, int *returnSize) {
356+
int n = strlen(boxes);
357+
int *left = malloc(sizeof(int) * n);
358+
int *right = malloc(sizeof(int) * n);
359+
memset(left, 0, sizeof(int) * n);
360+
memset(right, 0, sizeof(int) * n);
361+
for (int i = 1, count = 0; i < n; i++) {
362+
if (boxes[i - 1] == '1') {
363+
count++;
364+
}
365+
left[i] = left[i - 1] + count;
366+
}
367+
for (int i = n - 2, count = 0; i >= 0; i--) {
368+
if (boxes[i + 1] == '1') {
369+
count++;
370+
}
371+
right[i] = right[i + 1] + count;
372+
}
373+
int *ans = malloc(sizeof(int) * n);
374+
for (int i = 0; i < n; i++) {
375+
ans[i] = left[i] + right[i];
376+
}
377+
free(left);
378+
free(right);
379+
*returnSize = n;
380+
return ans;
381+
}
382+
```
383+
384+
```c
385+
/**
386+
* Note: The returned array must be malloced, assume caller calls free().
387+
*/
388+
int *minOperations(char *boxes, int *returnSize) {
389+
int n = strlen(boxes);
390+
int *ans = malloc(sizeof(int) * n);
391+
memset(ans, 0, sizeof(int) * n);
392+
for (int i = 1, count = 0; i < n; i++) {
393+
if (boxes[i - 1] == '1') {
394+
count++;
395+
}
396+
ans[i] = ans[i - 1] + count;
397+
}
398+
for (int i = n - 2, count = 0, sum = 0; i >= 0; i--) {
399+
if (boxes[i + 1] == '1') {
400+
count++;
401+
}
402+
sum += count;
403+
ans[i] += sum;
404+
}
405+
*returnSize = n;
406+
return ans;
407+
}
408+
```
409+
250410
### **...**
251411

252412
```

solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README_EN.md

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,166 @@ func minOperations(boxes string) []int {
229229
}
230230
```
231231

232+
### **TypeScript**
233+
234+
```ts
235+
function minOperations(boxes: string): number[] {
236+
const n = boxes.length;
237+
const left = new Array(n).fill(0);
238+
const right = new Array(n).fill(0);
239+
for (let i = 1, count = 0; i < n; i++) {
240+
if (boxes[i - 1] == '1') {
241+
count++;
242+
}
243+
left[i] = left[i - 1] + count;
244+
}
245+
for (let i = n - 2, count = 0; i >= 0; i--) {
246+
if (boxes[i + 1] == '1') {
247+
count++;
248+
}
249+
right[i] = right[i + 1] + count;
250+
}
251+
return left.map((v, i) => v + right[i]);
252+
}
253+
```
254+
255+
```ts
256+
function minOperations(boxes: string): number[] {
257+
const n = boxes.length;
258+
const ans = new Array(n).fill(0);
259+
for (let i = 1, count = 0; i < n; i++) {
260+
if (boxes[i - 1] === '1') {
261+
count++;
262+
}
263+
ans[i] = ans[i - 1] + count;
264+
}
265+
for (let i = n - 2, count = 0, sum = 0; i >= 0; i--) {
266+
if (boxes[i + 1] === '1') {
267+
count++;
268+
}
269+
sum += count;
270+
ans[i] += sum;
271+
}
272+
return ans;
273+
}
274+
```
275+
276+
### **Rust**
277+
278+
```rust
279+
impl Solution {
280+
pub fn min_operations(boxes: String) -> Vec<i32> {
281+
let s = boxes.as_bytes();
282+
let n = s.len();
283+
let mut left = vec![0; n];
284+
let mut right = vec![0; n];
285+
let mut count = 0;
286+
for i in 1..n {
287+
if s[i - 1] == b'1' {
288+
count += 1;
289+
}
290+
left[i] = left[i - 1] + count;
291+
}
292+
count = 0;
293+
for i in (0..n - 1).rev() {
294+
if s[i + 1] == b'1' {
295+
count += 1;
296+
}
297+
right[i] = right[i + 1] + count;
298+
}
299+
(0..n).into_iter().map(|i| left[i] + right[i]).collect()
300+
}
301+
}
302+
```
303+
304+
```rust
305+
impl Solution {
306+
pub fn min_operations(boxes: String) -> Vec<i32> {
307+
let s = boxes.as_bytes();
308+
let n = s.len();
309+
let mut ans = vec![0; n];
310+
let mut count = 0;
311+
for i in 1..n {
312+
if s[i - 1] == b'1' {
313+
count += 1;
314+
}
315+
ans[i] = ans[i - 1] + count;
316+
}
317+
let mut sum = 0;
318+
count = 0;
319+
for i in (0..n - 1).rev() {
320+
if s[i + 1] == b'1' {
321+
count += 1;
322+
}
323+
sum += count;
324+
ans[i] += sum;
325+
}
326+
ans
327+
}
328+
}
329+
```
330+
331+
### **C**
332+
333+
```c
334+
/**
335+
* Note: The returned array must be malloced, assume caller calls free().
336+
*/
337+
int *minOperations(char *boxes, int *returnSize) {
338+
int n = strlen(boxes);
339+
int *left = malloc(sizeof(int) * n);
340+
int *right = malloc(sizeof(int) * n);
341+
memset(left, 0, sizeof(int) * n);
342+
memset(right, 0, sizeof(int) * n);
343+
for (int i = 1, count = 0; i < n; i++) {
344+
if (boxes[i - 1] == '1') {
345+
count++;
346+
}
347+
left[i] = left[i - 1] + count;
348+
}
349+
for (int i = n - 2, count = 0; i >= 0; i--) {
350+
if (boxes[i + 1] == '1') {
351+
count++;
352+
}
353+
right[i] = right[i + 1] + count;
354+
}
355+
int *ans = malloc(sizeof(int) * n);
356+
for (int i = 0; i < n; i++) {
357+
ans[i] = left[i] + right[i];
358+
}
359+
free(left);
360+
free(right);
361+
*returnSize = n;
362+
return ans;
363+
}
364+
```
365+
366+
```c
367+
/**
368+
* Note: The returned array must be malloced, assume caller calls free().
369+
*/
370+
int *minOperations(char *boxes, int *returnSize) {
371+
int n = strlen(boxes);
372+
int *ans = malloc(sizeof(int) * n);
373+
memset(ans, 0, sizeof(int) * n);
374+
for (int i = 1, count = 0; i < n; i++) {
375+
if (boxes[i - 1] == '1') {
376+
count++;
377+
}
378+
ans[i] = ans[i - 1] + count;
379+
}
380+
for (int i = n - 2, count = 0, sum = 0; i >= 0; i--) {
381+
if (boxes[i + 1] == '1') {
382+
count++;
383+
}
384+
sum += count;
385+
ans[i] += sum;
386+
}
387+
*returnSize = n;
388+
return ans;
389+
}
390+
```
391+
232392
### **...**
233393

234394
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int *minOperations(char *boxes, int *returnSize) {
5+
int n = strlen(boxes);
6+
int *ans = malloc(sizeof(int) * n);
7+
memset(ans, 0, sizeof(int) * n);
8+
for (int i = 1, count = 0; i < n; i++) {
9+
if (boxes[i - 1] == '1') {
10+
count++;
11+
}
12+
ans[i] = ans[i - 1] + count;
13+
}
14+
for (int i = n - 2, count = 0, sum = 0; i >= 0; i--) {
15+
if (boxes[i + 1] == '1') {
16+
count++;
17+
}
18+
sum += count;
19+
ans[i] += sum;
20+
}
21+
*returnSize = n;
22+
return ans;
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn min_operations(boxes: String) -> Vec<i32> {
3+
let s = boxes.as_bytes();
4+
let n = s.len();
5+
let mut ans = vec![0; n];
6+
let mut count = 0;
7+
for i in 1..n {
8+
if s[i - 1] == b'1' {
9+
count += 1;
10+
}
11+
ans[i] = ans[i - 1] + count;
12+
}
13+
let mut sum = 0;
14+
count = 0;
15+
for i in (0..n - 1).rev() {
16+
if s[i + 1] == b'1' {
17+
count += 1;
18+
}
19+
sum += count;
20+
ans[i] += sum;
21+
}
22+
ans
23+
}
24+
}

0 commit comments

Comments
 (0)