Skip to content

Commit 363ad11

Browse files
committed
Sync LeetCode submission Runtime - 11 ms (68.21%), Memory - 17.7 MB (46.76%)
1 parent 161c151 commit 363ad11

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<p>You have <code>n</code> boxes. You are given a binary string <code>boxes</code> of length <code>n</code>, where <code>boxes[i]</code> is <code>&#39;0&#39;</code> if the <code>i<sup>th</sup></code> box is <strong>empty</strong>, and <code>&#39;1&#39;</code> if it contains <strong>one</strong> ball.</p>
2+
3+
<p>In one operation, you can move <strong>one</strong> ball from a box to an adjacent box. Box <code>i</code> is adjacent to box <code>j</code> if <code>abs(i - j) == 1</code>. Note that after doing so, there may be more than one ball in some boxes.</p>
4+
5+
<p>Return an array <code>answer</code> of size <code>n</code>, where <code>answer[i]</code> is the <strong>minimum</strong> number of operations needed to move all the balls to the <code>i<sup>th</sup></code> box.</p>
6+
7+
<p>Each <code>answer[i]</code> is calculated considering the <strong>initial</strong> state of the boxes.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> boxes = &quot;110&quot;
14+
<strong>Output:</strong> [1,1,3]
15+
<strong>Explanation:</strong> The answer for each box is as follows:
16+
1) First box: you will have to move one ball from the second box to the first box in one operation.
17+
2) Second box: you will have to move one ball from the first box to the second box in one operation.
18+
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
19+
</pre>
20+
21+
<p><strong class="example">Example 2:</strong></p>
22+
23+
<pre>
24+
<strong>Input:</strong> boxes = &quot;001011&quot;
25+
<strong>Output:</strong> [11,8,5,4,3,4]</pre>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
29+
30+
<ul>
31+
<li><code>n == boxes.length</code></li>
32+
<li><code>1 &lt;= n &lt;= 2000</code></li>
33+
<li><code>boxes[i]</code> is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
34+
</ul>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Approach 2: Sum of Left and Right Moves
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def minOperations(self, boxes: str) -> List[int]:
8+
n = len(boxes)
9+
answer = [0] * n
10+
11+
balls_to_left = 0
12+
moves_to_left = 0
13+
balls_to_right = 0
14+
moves_to_right = 0
15+
16+
for i in range(n):
17+
answer[i] += moves_to_left
18+
balls_to_left += int(boxes[i])
19+
moves_to_left += balls_to_left
20+
21+
j = n - 1 - i
22+
answer[j] += moves_to_right
23+
balls_to_right += int(boxes[j])
24+
moves_to_right += balls_to_right
25+
26+
return answer
27+

0 commit comments

Comments
 (0)