Skip to content

Commit 14a042b

Browse files
committed
Time: 1 ms (100.00%), Space: 64.5 MB (92.00%) - LeetHub
1 parent fab0342 commit 14a042b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int findLengthOfShortestSubarray(int[] arr) {
3+
int left = 0;
4+
while(left + 1 < arr.length && arr[left] <= arr[left+1]) left++;
5+
if(left == arr.length - 1) return 0;
6+
7+
int right = arr.length - 1;
8+
while(right > left && arr[right-1] <= arr[right]) right--;
9+
int result = Math.min(arr.length - left - 1, right);
10+
11+
int i = 0;
12+
int j = right;
13+
while(i <= left && j < arr.length) {
14+
if(arr[j] >= arr[i]) {
15+
result = Math.min(result, j - i - 1);
16+
i++;
17+
}else {
18+
j++;
19+
}
20+
}
21+
return result;
22+
}
23+
}

0 commit comments

Comments
 (0)