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. C - Monthly Expense

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

  2. 不同的GCD算法

    分类: C语言程序2014-10-08 15:10 28人阅读 评论(0) 收藏 举报 gcdC语言程序位运算 早在公元前300年左右,欧几里得就在他的著作<几何原本>中给出了高效的解法- ...

  3. ELK之从6.3.1升级至6.6.2

    需要把原6.3.1版本升级为6.6.2版本 1,官网下载rpm包 2,升级elasticsearch和kibana rpm -U elasticsearch-6.6.2.rpm rpm -U kiba ...

  4. WordOperate

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  5. java虚拟机学习

    //20181129 ·Java虚拟机的内存分为三个部分:栈stack.堆heap.方法区method area----包含在“堆”里面,因为作用特殊所以单独列出来 ·栈的特点:     栈描述的是方 ...

  6. LUA 语言易混点

    --代码: tab1 = { key1 = "val1", key2 = "val2","val2", "val3" , ...

  7. NLP任务:给定一句话,找出这句话中你想要的关键词,包括起始结束索引

    在实际的nlp实际任务中,你有一大堆的人工标注的关键词,来新的一句话,找出这句话中的关键词,以便你以后使用,那如何来做呢? 1)用到正则的 finditer()方法,返回你匹配的关键词的迭代对象,包含 ...

  8. flex-shrink (适用于弹性盒模型容器子元素)

    设置或检索弹性盒的收缩比率(根据弹性盒子元素所设置的收缩因子作为比率来收缩空间.) 语法 flex-shrink: <number> (default 1) flex-shrink的默认值 ...

  9. GatewayWorker

    GatewayWorker介绍 一.工作原理 Register.Gateway.BusinessWorker进程启动 Gateway.BusinessWorker进程启动后向Register服务进程发 ...

  10. CALayer的子类之CAShapeLayer

    一,CAShapeLayer介绍 * CAShapeLayer继承自CALayer,属于QuartzCore框架,可使用CALayer的所有属性.   CAShapeLayer是在坐标系内绘制贝塞尔曲 ...