Skip to content

Commit 2ad4950

Browse files
authored
Update 1769.Minimum-Number-of-Operations-to-Move-All-Balls-to-Each-Box.cpp
1 parent 98530e6 commit 2ad4950

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

Greedy/1769.Minimum-Number-of-Operations-to-Move-All-Balls-to-Each-Box/1769.Minimum-Number-of-Operations-to-Move-All-Balls-to-Each-Box.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ class Solution {
55
int n = boxes.size();
66
vector<int>leftMoves(n);
77
vector<int>rightMoves(n);
8-
98
int left = 0;
10-
for (int i=0; i<n; i++)
11-
{
12-
leftMoves[i] += (i==0 ? 0: leftMoves[i-1]) + left;
13-
left += (boxes[i]=='1');
9+
for (int i = 1; i<n; i++)
10+
{
11+
left += (boxes[i-1]=='1');
12+
leftMoves[i] = leftMoves[i-1] + left*1;
1413
}
1514
int right = 0;
16-
for (int i=n-1; i>=0; i--)
15+
for (int i = n-2; i>=0; i--)
1716
{
18-
rightMoves[i] += (i==n-1 ? 0: rightMoves[i+1]) + right;
19-
right += (boxes[i]=='1');
17+
right += (boxes[i+1]=='1');
18+
rightMoves[i] = rightMoves[i+1] + right*1;
2019
}
21-
2220
vector<int>rets(n);
2321
for (int i=0; i<n; i++)
24-
rets[i] = leftMoves[i] + rightMoves[i];
22+
rets[i] = leftMoves[i]+rightMoves[i];
2523
return rets;
2624
}
2725
};

0 commit comments

Comments
 (0)