Skip to content

Commit 8c120e5

Browse files
committed
Jan 6
1 parent be47284 commit 8c120e5

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def minOperations(self, boxes: str) -> List[int]:
6+
boxes = list(map(int, boxes))
7+
n = len(boxes)
8+
left_moves, right_moves = [0] * n, [0] * n
9+
10+
# compute weighted prefix and suffix sums
11+
count = boxes[0]
12+
for i in range(1, n):
13+
left_moves[i] = left_moves[i - 1] + count
14+
count += boxes[i]
15+
count = boxes[-1]
16+
for i in range(n - 2, -1, -1):
17+
right_moves[i] = right_moves[i + 1] + count
18+
count += boxes[i]
19+
return [l + r for l, r in zip(left_moves, right_moves)]
20+
21+
22+
def main():
23+
boxes = '110'
24+
assert Solution().minOperations(boxes) == [1, 1, 3]
25+
26+
boxes = '001011'
27+
assert Solution().minOperations(boxes) == [11, 8, 5, 4, 3, 4]
28+
29+
30+
if __name__ == '__main__':
31+
main()

2025-01-January-LeetCoding-Challenge/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
| January 3 | [2270. Number of Ways to Split Array](https://leetcode.com/problems/number-of-ways-to-split-array/) | Medium | Solved |
99
| January 4 | [1930. Unique Length-3 Palindromic Subsequences](https://leetcode.com/problems/unique-length-3-palindromic-subsequences/) | Medium | Solved |
1010
| January 5 | [2381. Shifting Letters II](https://leetcode.com/problems/shifting-letters-ii/) | Medium | Solved |
11-
| January 6 | []() | | |
11+
| January 6 | [1769. Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | Medium | Solved |
1212
| January 7 | []() | | |
1313
| January 8 | []() | | |
1414
| January 9 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 1 | 1 | 0 |
43-
| Medium | 4 | 4 | 0 |
43+
| Medium | 5 | 5 | 0 |
4444
| Hard | 0 | 0 | 0 |

0 commit comments

Comments
 (0)