Skip to content

Commit 468013a

Browse files
add 1574
1 parent 6f861ca commit 468013a

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-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+
|1574|[Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1574.java) ||Array, Binary Search|Medium|
1112
|1572|[Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1572.java) ||Array|Easy|
1213
|1567|[Maximum Length of Subarray With Positive Product](https://leetcode.com/problems/maximum-length-of-subarray-with-positive-product/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1567.java) |[:tv:](https://youtu.be/bFer5PdsgpY)|Greedy|Medium|
1314
|1566|[Detect Pattern of Length M Repeated K or More Times](https://leetcode.com/problems/detect-pattern-of-length-m-repeated-k-or-more-times/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1566.java) |[:tv:](https://youtu.be/aJAV_VgmjdE)|Array|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1574 {
4+
public static class Solution1 {
5+
public int findLengthOfShortestSubarray(int[] arr) {
6+
int left = 0;
7+
while (left < arr.length - 1 && arr[left] <= arr[left + 1]) {
8+
left++;
9+
}
10+
if (left == arr.length - 1) {
11+
return 0;
12+
}
13+
int right = arr.length - 1;
14+
while (right > left && arr[right] >= arr[right - 1]) {
15+
right--;
16+
}
17+
if (right == 0) {
18+
return arr.length - 1;
19+
}
20+
int result = Math.min(arr.length - left - 1, right);
21+
int i = 0;
22+
int j = right;
23+
while (i <= left && j < arr.length) {
24+
if (arr[j] >= arr[i]) {
25+
result = Math.min(result, j - i - 1);
26+
i++;
27+
} else {
28+
j++;
29+
}
30+
}
31+
return result;
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1574;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1574Test {
10+
private static _1574.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1574.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{1, 2, 3, 10, 4, 2, 3, 5};
21+
assertEquals(3, solution1.findLengthOfShortestSubarray(arr));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)