Skip to content

Commit 060e300

Browse files
committedDec 1, 2022
feat: add solutions to lc problem: No.1769
No.1769.Minimum Number of Operations to Move All Balls to Each Box
1 parent e7cee73 commit 060e300

File tree

7 files changed

+323
-153
lines changed

7 files changed

+323
-153
lines changed
 

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

+145-50
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
**方法一:预处理 + 枚举**
49+
50+
我们可以预处理出把每个位置 $i$ 左边的小球移动到 $i$ 的操作数,记为 $left[i]$;把每个位置 $i$ 右边的小球移动到 $i$ 的操作数,记为 $right[i]$。那么答案数组的第 $i$ 个元素就是 $left[i] + right[i]$。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `boxes` 的长度。
53+
54+
我们还可以进一步优化空间复杂度,只用一个答案数组 $ans$ 以及若干个变量即可。
55+
56+
时间复杂度 $O(n)$,忽略答案数组的空间复杂度,空间复杂度 $O(1)$。其中 $n$ 为 `boxes` 的长度。
57+
4858
<!-- tabs:start -->
4959

5060
### **Python3**
@@ -55,19 +65,38 @@
5565
class Solution:
5666
def minOperations(self, boxes: str) -> List[int]:
5767
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
68+
left = [0] * n
69+
right = [0] * n
70+
cnt = 0
71+
for i in range(1, n):
72+
if boxes[i - 1] == '1':
73+
cnt += 1
74+
left[i] = left[i - 1] + cnt
75+
cnt = 0
76+
for i in range(n - 2, -1, -1):
77+
if boxes[i + 1] == '1':
78+
cnt += 1
79+
right[i] = right[i + 1] + cnt
80+
return [a + b for a, b in zip(left, right)]
81+
```
82+
83+
```python
84+
class Solution:
85+
def minOperations(self, boxes: str) -> List[int]:
86+
n = len(boxes)
87+
ans = [0] * n
88+
cnt = 0
6589
for i in range(1, n):
6690
if boxes[i - 1] == '1':
67-
left += 1
68-
right -= 1
69-
res[i] = res[i - 1] + left - right
70-
return res
91+
cnt += 1
92+
ans[i] = ans[i - 1] + cnt
93+
cnt = s = 0
94+
for i in range(n - 2, -1, -1):
95+
if boxes[i + 1] == '1':
96+
cnt += 1
97+
s += cnt
98+
ans[i] += s
99+
return ans
71100
```
72101

73102
### **Java**
@@ -78,23 +107,48 @@ class Solution:
78107
class Solution {
79108
public int[] minOperations(String boxes) {
80109
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;
110+
int[] left = new int[n];
111+
int[] right = new int[n];
112+
for (int i = 1, cnt = 0; i < n; ++i) {
113+
if (boxes.charAt(i - 1) == '1') {
114+
++cnt;
115+
}
116+
left[i] = left[i - 1] + cnt;
117+
}
118+
for (int i = n - 2, cnt = 0; i >= 0; --i) {
119+
if (boxes.charAt(i + 1) == '1') {
120+
++cnt;
87121
}
122+
right[i] = right[i + 1] + cnt;
88123
}
89-
int left = 0, right = total;
90-
for (int i = 1; i < n; ++i) {
124+
int[] ans = new int[n];
125+
for (int i = 0; i < n; ++i) {
126+
ans[i] = left[i] + right[i];
127+
}
128+
return ans;
129+
}
130+
}
131+
```
132+
133+
```java
134+
class Solution {
135+
public int[] minOperations(String boxes) {
136+
int n = boxes.length();
137+
int[] ans = new int[n];
138+
for (int i = 1, cnt = 0; i < n; ++i) {
91139
if (boxes.charAt(i - 1) == '1') {
92-
++left;
93-
--right;
140+
++cnt;
94141
}
95-
res[i] = res[i - 1] + left - right;
142+
ans[i] = ans[i - 1] + cnt;
96143
}
97-
return res;
144+
for (int i = n - 2, cnt = 0, s = 0; i >= 0; --i) {
145+
if (boxes.charAt(i + 1) == '1') {
146+
++cnt;
147+
}
148+
s += cnt;
149+
ans[i] += s;
150+
}
151+
return ans;
98152
}
99153
}
100154
```
@@ -106,23 +160,41 @@ class Solution {
106160
public:
107161
vector<int> minOperations(string boxes) {
108162
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-
}
163+
int left[n];
164+
int right[n];
165+
memset(left, 0, sizeof left);
166+
memset(right, 0, sizeof right);
167+
for (int i = 1, cnt = 0; i < n; ++i) {
168+
cnt += boxes[i - 1] == '1';
169+
left[i] = left[i - 1] + cnt;
116170
}
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;
171+
for (int i = n - 2, cnt = 0; ~i; --i) {
172+
cnt += boxes[i + 1] == '1';
173+
right[i] = right[i + 1] + cnt;
124174
}
125-
return res;
175+
vector<int> ans(n);
176+
for (int i = 0; i < n; ++i) ans[i] = left[i] + right[i];
177+
return ans;
178+
}
179+
};
180+
```
181+
182+
```cpp
183+
class Solution {
184+
public:
185+
vector<int> minOperations(string boxes) {
186+
int n = boxes.size();
187+
vector<int> ans(n);
188+
for (int i = 1, cnt = 0; i < n; ++i) {
189+
cnt += boxes[i - 1] == '1';
190+
ans[i] = ans[i - 1] + cnt;
191+
}
192+
for (int i = n - 2, cnt = 0, s = 0; ~i; --i) {
193+
cnt += boxes[i + 1] == '1';
194+
s += cnt;
195+
ans[i] += s;
196+
}
197+
return ans;
126198
}
127199
};
128200
```
@@ -132,23 +204,46 @@ public:
132204
```go
133205
func minOperations(boxes string) []int {
134206
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++
207+
left := make([]int, n)
208+
right := make([]int, n)
209+
for i, cnt := 1, 0; i < n; i++ {
210+
if boxes[i-1] == '1' {
211+
cnt++
212+
}
213+
left[i] = left[i-1] + cnt
214+
}
215+
for i, cnt := n-2, 0; i >= 0; i-- {
216+
if boxes[i+1] == '1' {
217+
cnt++
141218
}
219+
right[i] = right[i+1] + cnt
142220
}
143-
left, right := 0, total
144-
for i := 1; i < n; i++ {
221+
ans := make([]int, n)
222+
for i := range ans {
223+
ans[i] = left[i] + right[i]
224+
}
225+
return ans
226+
}
227+
```
228+
229+
```go
230+
func minOperations(boxes string) []int {
231+
n := len(boxes)
232+
ans := make([]int, n)
233+
for i, cnt := 1, 0; i < n; i++ {
145234
if boxes[i-1] == '1' {
146-
left++
147-
right--
235+
cnt++
236+
}
237+
ans[i] = ans[i-1] + cnt
238+
}
239+
for i, cnt, s := n-2, 0, 0; i >= 0; i-- {
240+
if boxes[i+1] == '1' {
241+
cnt++
148242
}
149-
res[i] = res[i-1] + left - right
243+
s += cnt
244+
ans[i] += s
150245
}
151-
return res
246+
return ans
152247
}
153248
```
154249

0 commit comments

Comments
 (0)