Skip to content

Commit 7e951d3

Browse files
committedApr 20, 2025
Add solution #1574
1 parent ee13fd4 commit 7e951d3

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,377 LeetCode solutions in JavaScript
1+
# 1,378 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1201,6 +1201,7 @@
12011201
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|
12021202
1572|[Matrix Diagonal Sum](./solutions/1572-matrix-diagonal-sum.js)|Easy|
12031203
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|
12041205
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
12051206
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12061207
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.