-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path1769.java
More file actions
29 lines (26 loc) · 755 Bytes
/
1769.java
File metadata and controls
29 lines (26 loc) · 755 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public int[] minOperations(String boxes) {
int n = boxes.length();
int[] res = new int[n];
//calculate the operations needed from left to right
int count=0, sum=0;
for(int i=1;i<n;i++) {
if(boxes.charAt(i - 1) == '1'){
count++;
}
sum += count;
res[i] = sum;
}
//calculate the operations needed from right to left
count = 0;
sum = 0;
for(int i=n-2;i>=0;i--) {
if(boxes.charAt(i + 1) == '1'){
count++;
}
sum += count;
res[i] += sum;
}
return res;
}
}