File tree 2 files changed +45
-1
lines changed
2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,377 LeetCode solutions in JavaScript
1
+ # 1,378 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1201
1201
1569|[ Number of Ways to Reorder Array to Get Same BST] ( ./solutions/1569-number-of-ways-to-reorder-array-to-get-same-bst.js ) |Hard|
1202
1202
1572|[ Matrix Diagonal Sum] ( ./solutions/1572-matrix-diagonal-sum.js ) |Easy|
1203
1203
1573|[ Number of Ways to Split a String] ( ./solutions/1573-number-of-ways-to-split-a-string.js ) |Medium|
1204
+ 1574|[ Shortest Subarray to be Removed to Make Array Sorted] ( ./solutions/1574-shortest-subarray-to-be-removed-to-make-array-sorted.js ) |Medium|
1204
1205
1576|[ Replace All ?'s to Avoid Consecutive Repeating Characters] ( ./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js ) |Medium|
1205
1206
1598|[ Crawler Log Folder] ( ./solutions/1598-crawler-log-folder.js ) |Easy|
1206
1207
1657|[ Determine if Two Strings Are Close] ( ./solutions/1657-determine-if-two-strings-are-close.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1574. Shortest Subarray to be Removed to Make Array Sorted
3
+ * https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer array arr, remove a subarray (can be empty) from arr such that the remaining
7
+ * elements in arr are non-decreasing.
8
+ *
9
+ * Return the length of the shortest subarray to remove.
10
+ *
11
+ * A subarray is a contiguous subsequence of the array.
12
+ */
13
+
14
+ /**
15
+ * @param {number[] } arr
16
+ * @return {number }
17
+ */
18
+ var findLengthOfShortestSubarray = function ( arr ) {
19
+ const n = arr . length ;
20
+ let left = 0 ;
21
+
22
+ while ( left < n - 1 && arr [ left ] <= arr [ left + 1 ] ) {
23
+ left ++ ;
24
+ }
25
+
26
+ if ( left === n - 1 ) return 0 ;
27
+
28
+ let right = n - 1 ;
29
+ while ( right > 0 && arr [ right - 1 ] <= arr [ right ] ) {
30
+ right -- ;
31
+ }
32
+
33
+ let minLength = Math . min ( n - left - 1 , right ) ;
34
+
35
+ for ( let i = 0 , j = right ; i <= left && j < n ; i ++ ) {
36
+ while ( j < n && arr [ i ] > arr [ j ] ) {
37
+ j ++ ;
38
+ }
39
+ minLength = Math . min ( minLength , j - i - 1 ) ;
40
+ }
41
+
42
+ return minLength ;
43
+ } ;
You can’t perform that action at this time.
0 commit comments