Skip to content

Commit e48cc32

Browse files
authored
Create Solution.js
1 parent 358c95a commit e48cc32

File tree

1 file changed

+41
-0
lines changed
  • solution/1500-1599/1574.Shortest Subarray to be Removed to Make Array Sorted

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number[]} arr
3+
* @return {number}
4+
*/
5+
var findLengthOfShortestSubarray = function(arr) {
6+
const n = arr.length;
7+
let i = 0, j = n - 1;
8+
9+
while (i + 1 < n && arr[i] <= arr[i + 1]) {
10+
i++;
11+
}
12+
13+
while (j - 1 >= 0 && arr[j - 1] <= arr[j]) {
14+
j--;
15+
}
16+
17+
if (i >= j) {
18+
return 0;
19+
}
20+
21+
let ans = Math.min(n - i - 1, j);
22+
23+
for (let l = 0; l <= i; l++) {
24+
const r = bisectLeft(arr, arr[l], j, n);
25+
ans = Math.min(ans, r - l - 1);
26+
}
27+
28+
return ans;
29+
};
30+
31+
let bisectLeft = (arr, x, lo, hi)=>{
32+
while (lo < hi) {
33+
const mid = Math.floor((lo + hi) / 2);
34+
if (arr[mid] < x) {
35+
lo = mid + 1;
36+
} else {
37+
hi = mid;
38+
}
39+
}
40+
return lo;
41+
}

0 commit comments

Comments
 (0)