We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a9d02a6 commit 45280ddCopy full SHA for 45280dd
581-shortest-unsorted-continuous-subarray.js
@@ -0,0 +1,25 @@
1
+/**
2
+ * @param {number[]} nums
3
+ * @return {number}
4
+ */
5
+const findUnsortedSubarray = function(nums) {
6
+ let stack = []
7
+ let left = nums.length
8
+ let right = 0
9
+ for(let i = 0; i < nums.length; i++) {
10
+ while(stack.length > 0 && nums[stack[stack.length - 1]] > nums[i]) {
11
+ left = Math.min(left, stack.pop())
12
+ }
13
+ stack.push(i)
14
15
+ stack = []
16
+ for(let i = nums.length - 1; i >= 0; i--) {
17
+ while(stack.length > 0 && nums[stack[stack.length - 1]] < nums[i] ) {
18
+ right = Math.max(right, stack.pop())
19
20
21
22
+
23
24
+ return right > left ? right - left + 1: 0
25
+};
0 commit comments