Skip to content

Commit 2bd1d57

Browse files
add 1526
1 parent 74d4cab commit 2bd1d57

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1526|[Minimum Number of Increments on Subarrays to Form a Target Array](https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1526.java) | |Hard|Segment Tree|
1112
|1525|[Number of Good Ways to Split a String](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1525.java) | |Medium|String, Bit Manipulation|
1213
|1524|[Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1524.java) | |Medium|Array, Math|
1314
|1523|[Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1523.java) | |Easy|Math|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1526 {
4+
public static class Solution1 {
5+
/**
6+
* This brute force solution results in TLE on LeetCode.
7+
*/
8+
public int minNumberOperations(int[] target) {
9+
int ops = 0;
10+
while (!allZero(target)) {
11+
int i = 0;
12+
while (target[i] == 0) {
13+
i++;
14+
}
15+
for (; i < target.length && target[i] != 0; i++) {
16+
target[i]--;
17+
}
18+
ops++;
19+
}
20+
return ops;
21+
}
22+
23+
private boolean allZero(int[] target) {
24+
for (int i : target) {
25+
if (i != 0) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1526;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1526Test {
10+
private static _1526.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1526.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(3, solution1.minNumberOperations(new int[]{1, 2, 3, 2, 1}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(4, solution1.minNumberOperations(new int[]{3, 1, 1, 2}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(7, solution1.minNumberOperations(new int[]{3, 1, 5, 4, 2}));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)