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. DOM内容操作

    <table border="2"> <thead id="1" class="c1 c2"> <tr> ...

  2. Sqlite数据库管理(SQLite Developer) v4.0.0.528 中文版+破解方法(申明:来源于网络)

    Sqlite数据库管理(SQLite Developer) v4.0.0.528 中文破解版 SharpPlus Sqlite Developer 特性: -强大的SQL编辑器 *Sqlite Sql ...

  3. cuteftp9破解及安装、使用

    一.破解: 参考:https://jingyan.baidu.com/article/ca00d56c4e43b2e99febcf70.html 1. 首先下载cuteftp替换文件及cuteftp9 ...

  4. 记一次 windows server 2012R2 上安装 MSSQL2005 及网站发布

    背景: 原因是因为没有购买新的数据库授权.由于之前服务器是windows 2003 由于后绪网站的版本可能会升到.net4.5 据说 2003上安装不起 所有由另一同事到客户安装一个window se ...

  5. ajax传输中文参数乱码,本地使用tomcat不乱码,liunx+weblogic乱码

    公司项目有个问题,ajax请求含中文,无论是post方式还是get方式.本地使用tomcat不乱码,liunx+weblogic都乱码.并且用以往encodeURIComponent()并在后台解码之 ...

  6. .NET Core开发日志——Model Binding

    ASP.NET Core MVC中所提供的Model Binding功能简单但实用,其主要目的是将请求中包含的数据映射到action的方法参数中.这样就避免了开发者像在Web Forms时代那样需要从 ...

  7. Windows小技巧 -- 目录内打开CMD的快捷方式

    工作中常常会有需要在某个文件夹内使用cmd的情况,例如运行某脚本,下面演示几种方法. 以进入以下目录操作为例: 方式一 : 常用的cd命令 cd命令是我们平常使用比较多的方式: 1. Win+R打开c ...

  8. mysql设置指定ip访问,用户权限相关操作

    基础语法GRANT priv_type ON database.table TO user[IDENTIFIED BY [PASSWORD] 'password'] [,user [IDENTIFIE ...

  9. 内部排序->插入排序->希尔排序

    文字描述 希尔排序又称缩小增量排序,也属于插入排序类,但在时间效率上较之前的插入排序有较大的改进. 从之前的直接插入排序的分析得知,时间复杂度为n*n, 有如下两个特点: (1)如果待排序记录本身就是 ...

  10. TensorFlow设置GPU占用量

    默认开启Tensorflow的session之后,就会占用几乎所有的显存,进行如下设置即可: 指定GPU编号: import os os.environ["CUDA_VISIBLE_DEVI ...