forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1526.java
32 lines (30 loc) · 824 Bytes
/
_1526.java
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
30
31
32
package com.fishercoder.solutions;
public class _1526 {
public static class Solution1 {
/**
* This brute force solution results in TLE on LeetCode.
*/
public int minNumberOperations(int[] target) {
int ops = 0;
while (!allZero(target)) {
int i = 0;
while (target[i] == 0) {
i++;
}
for (; i < target.length && target[i] != 0; i++) {
target[i]--;
}
ops++;
}
return ops;
}
private boolean allZero(int[] target) {
for (int i : target) {
if (i != 0) {
return false;
}
}
return true;
}
}
}