File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
solution/1500-1599/1574.Shortest Subarray to be Removed to Make Array Sorted Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments