581. Shortest Unsorted Continuous Subarray Medium

@problem@discussion
#Array#Two Pointers#Stack#Greedy#Sorting#Monotonic Stack



1/**
2 * [581] Shortest Unsorted Continuous Subarray
3 *
4 * Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.
5 * Return the shortest such subarray and output its length.
6 *  
7 * Example 1:
8 * 
9 * Input: nums = [2,6,4,8,10,9,15]
10 * Output: 5
11 * Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
12 * 
13 * Example 2:
14 * 
15 * Input: nums = [1,2,3,4]
16 * Output: 0
17 * 
18 * Example 3:
19 * 
20 * Input: nums = [1]
21 * Output: 0
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	1 <= nums.length <= 10^4
27 * 	-10^5 <= nums[i] <= 10^5
28 * 
29 *  
30 * Follow up: Can you solve it in O(n) time complexity?
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/shortest-unsorted-continuous-subarray/
35// discuss: https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40    pub fn find_unsorted_subarray(nums: Vec<i32>) -> i32 {
41        0
42    }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_581() {
53    }
54}
55


Back
© 2025 bowen.ge All Rights Reserved.