Skip to content

Commit 0268b5d

Browse files
committed
feat: add solutions to lc problem: No.1769. Minimum Number of Operations
to Move All Balls to Each Box
1 parent 7563cb1 commit 0268b5d

File tree

6 files changed

+261
-4
lines changed

6 files changed

+261
-4
lines changed

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

+90-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<li><code>boxes[i]</code> 为 <code>'0'</code> 或 <code>'1'</code></li>
4242
</ul>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
@@ -53,15 +52,104 @@
5352
<!-- 这里可写当前语言的特殊实现逻辑 -->
5453

5554
```python
56-
55+
class Solution:
56+
def minOperations(self, boxes: str) -> List[int]:
57+
n = len(boxes)
58+
res = [0] * n
59+
total = 0
60+
for i, b in enumerate(boxes):
61+
if b == '1':
62+
res[0] += i
63+
total += 1
64+
left, right = 0, total
65+
for i in range(1, n):
66+
if boxes[i - 1] == '1':
67+
left += 1
68+
right -= 1
69+
res[i] = res[i - 1] + left - right
70+
return res
5771
```
5872

5973
### **Java**
6074

6175
<!-- 这里可写当前语言的特殊实现逻辑 -->
6276

6377
```java
78+
class Solution {
79+
public int[] minOperations(String boxes) {
80+
int n = boxes.length();
81+
int[] res = new int[n];
82+
int total = 0;
83+
for (int i = 0; i < n; ++i) {
84+
if (boxes.charAt(i) == '1') {
85+
res[0] += i;
86+
++total;
87+
}
88+
}
89+
int left = 0, right = total;
90+
for (int i = 1; i < n; ++i) {
91+
if (boxes.charAt(i - 1) == '1') {
92+
++left;
93+
--right;
94+
}
95+
res[i] = res[i - 1] + left - right;
96+
}
97+
return res;
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
vector<int> minOperations(string boxes) {
108+
int n = boxes.size();
109+
vector<int> res(n);
110+
int total = 0;
111+
for (int i = 0; i < n; ++i) {
112+
if (boxes[i] == '1') {
113+
res[0] += i;
114+
++total;
115+
}
116+
}
117+
int left = 0, right = total;
118+
for (int i = 1; i < n; ++i) {
119+
if (boxes[i - 1] == '1') {
120+
++left;
121+
--right;
122+
}
123+
res[i] = res[i - 1] + left - right;
124+
}
125+
return res;
126+
}
127+
};
128+
```
64129
130+
### **Go**
131+
132+
```go
133+
func minOperations(boxes string) []int {
134+
n := len(boxes)
135+
res := make([]int, n)
136+
total := 0
137+
for i, b := range boxes {
138+
if b == '1' {
139+
res[0] += i
140+
total++
141+
}
142+
}
143+
left, right := 0, total
144+
for i := 1; i < n; i++ {
145+
if boxes[i-1] == '1' {
146+
left++
147+
right--
148+
}
149+
res[i] = res[i-1] + left - right
150+
}
151+
return res
152+
}
65153
```
66154

67155
### **...**

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

+90-2
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,109 @@
3939
<li><code>boxes[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
4040
</ul>
4141

42-
4342
## Solutions
4443

4544
<!-- tabs:start -->
4645

4746
### **Python3**
4847

4948
```python
50-
49+
class Solution:
50+
def minOperations(self, boxes: str) -> List[int]:
51+
n = len(boxes)
52+
res = [0] * n
53+
total = 0
54+
for i, b in enumerate(boxes):
55+
if b == '1':
56+
res[0] += i
57+
total += 1
58+
left, right = 0, total
59+
for i in range(1, n):
60+
if boxes[i - 1] == '1':
61+
left += 1
62+
right -= 1
63+
res[i] = res[i - 1] + left - right
64+
return res
5165
```
5266

5367
### **Java**
5468

5569
```java
70+
class Solution {
71+
public int[] minOperations(String boxes) {
72+
int n = boxes.length();
73+
int[] res = new int[n];
74+
int total = 0;
75+
for (int i = 0; i < n; ++i) {
76+
if (boxes.charAt(i) == '1') {
77+
res[0] += i;
78+
++total;
79+
}
80+
}
81+
int left = 0, right = total;
82+
for (int i = 1; i < n; ++i) {
83+
if (boxes.charAt(i - 1) == '1') {
84+
++left;
85+
--right;
86+
}
87+
res[i] = res[i - 1] + left - right;
88+
}
89+
return res;
90+
}
91+
}
92+
```
93+
94+
### **C++**
95+
96+
```cpp
97+
class Solution {
98+
public:
99+
vector<int> minOperations(string boxes) {
100+
int n = boxes.size();
101+
vector<int> res(n);
102+
int total = 0;
103+
for (int i = 0; i < n; ++i) {
104+
if (boxes[i] == '1') {
105+
res[0] += i;
106+
++total;
107+
}
108+
}
109+
int left = 0, right = total;
110+
for (int i = 1; i < n; ++i) {
111+
if (boxes[i - 1] == '1') {
112+
++left;
113+
--right;
114+
}
115+
res[i] = res[i - 1] + left - right;
116+
}
117+
return res;
118+
}
119+
};
120+
```
56121
122+
### **Go**
123+
124+
```go
125+
func minOperations(boxes string) []int {
126+
n := len(boxes)
127+
res := make([]int, n)
128+
total := 0
129+
for i, b := range boxes {
130+
if b == '1' {
131+
res[0] += i
132+
total++
133+
}
134+
}
135+
left, right := 0, total
136+
for i := 1; i < n; i++ {
137+
if boxes[i-1] == '1' {
138+
left++
139+
right--
140+
}
141+
res[i] = res[i-1] + left - right
142+
}
143+
return res
144+
}
57145
```
58146

59147
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<int> minOperations(string boxes) {
4+
int n = boxes.size();
5+
vector<int> res(n);
6+
int total = 0;
7+
for (int i = 0; i < n; ++i) {
8+
if (boxes[i] == '1') {
9+
res[0] += i;
10+
++total;
11+
}
12+
}
13+
int left = 0, right = total;
14+
for (int i = 1; i < n; ++i) {
15+
if (boxes[i - 1] == '1') {
16+
++left;
17+
--right;
18+
}
19+
res[i] = res[i - 1] + left - right;
20+
}
21+
return res;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func minOperations(boxes string) []int {
2+
n := len(boxes)
3+
res := make([]int, n)
4+
total := 0
5+
for i, b := range boxes {
6+
if b == '1' {
7+
res[0] += i
8+
total++
9+
}
10+
}
11+
left, right := 0, total
12+
for i := 1; i < n; i++ {
13+
if boxes[i-1] == '1' {
14+
left++
15+
right--
16+
}
17+
res[i] = res[i-1] + left - right
18+
}
19+
return res
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int[] minOperations(String boxes) {
3+
int n = boxes.length();
4+
int[] res = new int[n];
5+
int total = 0;
6+
for (int i = 0; i < n; ++i) {
7+
if (boxes.charAt(i) == '1') {
8+
res[0] += i;
9+
++total;
10+
}
11+
}
12+
int left = 0, right = total;
13+
for (int i = 1; i < n; ++i) {
14+
if (boxes.charAt(i - 1) == '1') {
15+
++left;
16+
--right;
17+
}
18+
res[i] = res[i - 1] + left - right;
19+
}
20+
return res;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def minOperations(self, boxes: str) -> List[int]:
3+
n = len(boxes)
4+
res = [0] * n
5+
total = 0
6+
for i, b in enumerate(boxes):
7+
if b == '1':
8+
res[0] += i
9+
total += 1
10+
left, right = 0, total
11+
for i in range(1, n):
12+
if boxes[i - 1] == '1':
13+
left += 1
14+
right -= 1
15+
res[i] = res[i - 1] + left - right
16+
return res

0 commit comments

Comments
 (0)