Skip to content

Commit c013f13

Browse files
add 2970
1 parent c44bfbb commit c013f13

File tree

3 files changed

+68
-0
lines changed
  • paginated_contents/algorithms/3rd_thousand
  • src

3 files changed

+68
-0
lines changed

Diff for: paginated_contents/algorithms/3rd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|---------------------------|----------------------------------------------------------------------
3+
| 2970 | [Count the Number of Incremovable Subarrays I](https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2970.java) | | Easy |
34
| 2965 | [Find Missing and Repeated Values](https://leetcode.com/problems/find-missing-and-repeated-values/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2965.java) | | Easy |
45
| 2864 | [Maximum Odd Binary Number](https://leetcode.com/problems/maximum-odd-binary-number/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2864.java) | | Easy |Greedy
56
| 2824 | [Count Pairs Whose Sum is Less than Target](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/thirdthousand/_2824.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder.solutions.thirdthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _2970 {
7+
public static class Solution1 {
8+
public int incremovableSubarrayCount(int[] nums) {
9+
int count = 0;
10+
for (int i = 0; i < nums.length; i++) {
11+
for (int j = i; j < nums.length; j++) {
12+
if (strictlyInc(nums, i, j)) {
13+
count++;
14+
}
15+
}
16+
}
17+
return count;
18+
}
19+
20+
private boolean strictlyInc(int[] nums, int start, int finish) {
21+
List<Integer> list = new ArrayList<>();
22+
for (int i = 0; i < start; i++) {
23+
list.add(nums[i]);
24+
}
25+
for (int i = finish + 1; i < nums.length; i++) {
26+
list.add(nums[i]);
27+
}
28+
for (int i = 0; i < list.size() - 1; i++) {
29+
if (list.get(i) >= list.get(i + 1)) {
30+
return false;
31+
}
32+
}
33+
return true;
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.thirdthousand;
2+
3+
import com.fishercoder.solutions.thirdthousand._2970;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _2970Test {
10+
private static _2970.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _2970.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(7, solution1.incremovableSubarrayCount(new int[]{6, 5, 7, 8}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.incremovableSubarrayCount(new int[]{8, 7, 6, 6}));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(3, solution1.incremovableSubarrayCount(new int[]{8, 7, 6, 6}));
30+
}
31+
}

0 commit comments

Comments
 (0)