Skip to content

Commit

Permalink
最短无序连续子数组
Browse files Browse the repository at this point in the history
  • Loading branch information
xldeng committed Jun 4, 2021
1 parent e2bab73 commit 9185248
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@

[leetcode地址](https://leetcode.com/problems/word-break/submissions/)

15. [每日温度--给一个表示温度的整数数组,输出每天需要等待比其更高温出现的天数](https://github.com/dengshasha/algorithm-study/blob/master/array/dailyTemperatures.js)

[leetcode地址](https://leetcode-cn.com/problems/daily-temperatures/)

16. [最短无序连续子数组](https://github.com/dengshasha/algorithm-study/blob/master/array/findUnsortedSubarray.js)

[leetcode地址](https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/)

### 动态规划
1. [强盗偷东西,价值最大化](https://github.com/dengshasha/algorithm-study/blob/master/dynamicProgramming/houseRobber.js)
[leetcode地址](https://leetcode.com/problems/house-robber/)
Expand Down
27 changes: 27 additions & 0 deletions array/findUnsortedSubarray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @param {number[]} nums
* @return {number}
* leetcode地址:https://leetcode-cn.com/problems/shortest-unsorted-continuous-subarray/
*/
var findUnsortedSubarray = function(nums) {
let start = Infinity, end = 0
let stack = []
for(let i = 0, len = nums.length; i < len; i++) {
while(stack.length && nums[stack[stack.length-1]] > nums[i]) {
start = Math.min(start, stack.pop())
}
stack.push(i)
}
stack = []
for(let i = nums.length-1; i >= 0; i--) {
while(stack.length && nums[stack[stack.length-1]] < nums[i]) {
end = Math.max(stack.pop(), end)
}
stack.push(i)
}
return end - start > 0 ? end - start + 1 : 0
};
console.log('2:',findUnsortedSubarray([2,1]))
console.log('0:',findUnsortedSubarray([1,2,3,3,3]))
console.log('5:',findUnsortedSubarray([1,3,4,2,2,2]))
console.log('5:',findUnsortedSubarray([2,6,4,8,10,9,15]))
12 changes: 12 additions & 0 deletions dsu/courseSchedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {number} numCourses
* @param {number[][]} prerequisites
* @return {boolean}
*/
var canFinish = function(numCourses, prerequisites) {
for(let i = 0; i < prerequisites.length; i++) {
for(let j = 0; j < prerequisites[i].length; j++) {

}
}
};
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<title>Document</title>
</head>
<body>
<script src="dynamicProgramming/wordBreak.js"></script>
<script src="array/findUnsortedSubarray.js"></script>
</body>
</html>

0 comments on commit 9185248

Please sign in to comment.