Given an integer array, 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, too.

You need to find the shortest such subarray and output its length.

Example 1:

Input: [2, 6, 4, 8, 10, 9, 15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.

Note:

  1. Then length of the input array is in range [1, 10,000].
  2. The input array may contain duplicates, so ascending order here means <=.

这个题目用sort去跟之前的nums比较, 然后把第一个不相等和最后一个不相等的index找到, return r - l + 1如果>1的话, 否则为0.

其次利用Stack, 把第一个不和谐的index找到, 再将nums reverse, 去找最后一个不和谐的index, return r - l + 1如果>1的话, 否则为0.

Code

1) Sort     T: O(nlgn)    S: O(n)

class Solution:
def findShortestSubarry(self, nums):
nums_copy, l, r, lr = sorted(nums), len(nums)-1, 0, len(nums)
for i in range(lr):
if nums[i] != nums_copy[i]:
l = min(l, i)
r = max(r, i)
ans = r-l + 1
return ans if ans > 0 else 0

2. Stack   T: O(n)    S; O(n)

class Solution:
def findShortestSubarry(self, nums):
stack, l, r, lr = [], len(nums)-1, 0, len(nums)
for i in range(lr):
while stack and nums[stack[-1]] > nums[i]:
l = min(l, stack.pop())
stack.append(i)
stack = []
for i in range(lr)[::-1]:
while stack and nums[stack[-1]] < nums[i]:
r = max(r, stack.pop())
stack.append(i)
ans = r - l + 1
return ans if ans > 0 else 0

[LeetCode] 581. Shortest Unsorted Continuous Subarray_Easy tag: Sort, Stack的更多相关文章

  1. LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  2. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

    problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...

  3. 【LeetCode】581. Shortest Unsorted Continuous Subarray 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:排序比较 日期 题目地址:https://leetco ...

  4. 【leetcode】581. Shortest Unsorted Continuous Subarray

    题目如下: 解题思路:本题我采用的是最简单最直接最粗暴的方法,把排序后的nums数组和原始数组比较即可得到答案. 代码如下: /** * @param {number[]} nums * @retur ...

  5. 581. Shortest Unsorted Continuous Subarray

      Given an integer array, you need to find one continuous subarray that if you only sort this subarr ...

  6. 581. Shortest Unsorted Continuous Subarray连续数组中的递增异常情况

    [抄题]: Given an integer array, you need to find one continuous subarray that if you only sort this su ...

  7. LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)

    581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...

  8. [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  9. LeetCode Shortest Unsorted Continuous Subarray

    原题链接在这里:https://leetcode.com/problems/shortest-unsorted-continuous-subarray/description/ 题目: Given a ...

随机推荐

  1. B - Hamburgers

    Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own han ...

  2. ConcurrentLinkedQueue since java1.5

    1 父类 java.lang.Object 继承者 java.util.AbstractCollection<E> 继承者 java.util.AbstractQueue<E> ...

  3. 洛谷P1118 数字三角形【dfs】【STL】

    题目链接:https://www.luogu.org/problemnew/show/P1118 题意: 1~n的一个排列,相邻的两项加起来得到下一行. 现在给定最后一行的数字,问最初的1~n的排列是 ...

  4. TOP100summit:【分享实录-途牛】价格中心系统的优化之路

    本篇文章内容来自2016年TOP100summit途牛旅游网高级架构师,技术委员会开发组长赵国光的案例分享.编辑:Cynthia 导读:价格中心系统是途牛网众多系统中很重要的一个,几乎所有的售卖价格计 ...

  5. .NET Core开发日志——结构化日志

    在.NET生态圈中,最早被广泛使用的日志库可能是派生自Java世界里的Apache log4net.而其后来者,莫过于NLog.Nlog与log4net相比,有一项较显著的优势,它支持结构化日志. 结 ...

  6. 用C# 7.0的switch...case模式匹配取代一堆if语句

    今天在重构代码时对下面的一堆if语句实在看着不顺眼. if(activation == null) { _logger.LogError("x1"); return Boolean ...

  7. VMware与Centos系统安装 和重置root密码

    VMware与Centos系统安装   今日任务 1.Linux发行版的选择 2.vmware创建一个虚拟机(centos) 3.安装配置centos7 4.xshell配置连接虚拟机(centos) ...

  8. Codeforces 670E - Correct Bracket Sequence Editor - [线段树]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  9. java8集合--LinkedList纯源码

    package Queue; import java.util.*; import java.util.function.Consumer; /** * 双端队列主要实现list接口和Deque接口, ...

  10. 模板倍增LCA 求树上两点距离 hdu2586

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 课上给的ppt里的模板是错的,wa了一下午orz.最近总是被坑啊... 题解:树上两点距离转化为到根的距离 ...