当前位置:网站首页>shortest-unsorted-continuous-subarray
shortest-unsorted-continuous-subarray
2022-07-25 10:18:00 【scwMason】
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:
- Then length of the input array is in range [1, 10,000].
- The input array may contain duplicates, so ascending order here means <=.
First sort the original sequence , Then compare with the original sequence , Look at the sequence before and after which start is different from the sorted sequence , And then subtract :
For example, in the above example
2, 6, 4, 8, 10, 9, 15
After sorting
2,4,6,8,9,10,15
So start from 4 The beginning is the first 2 A different , It starts later 10 Different , That is the first. 6 position , therefore 6-2+1=5
therefore :
class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
nums2=sorted(nums)
right=0
left=-1
for index in range(0,len(nums)):
if nums[index]!=nums2[index]:
left=index
for index in range(len(nums)-1,-1,-1):
if nums[index]!=nums2[index]:
right=index
return left-right+1Because I didn't know before sorted This method can return the sorted sequence , So two deep copies are used (copy.deepcopy) Resulting in slow running speed .
边栏推荐
猜你喜欢

复现 ASVspoof 2021 baseline RawNet2

Angr(九)——angr_ctf

Common methods of nodejs version upgrade or switching

mysql 解决不支持中文的问题

复现 SSL_Anti-spoofing, 使用 wav2vec 2.0 和数据增强的自动说话人认证的欺骗攻击与深度伪造检测

UE4源码的获取和编译

message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“

Redis使用场景

数论--约数研究

conda 配置深度学习环境 pytorch transformers
随机推荐
【专栏】RPC系列(理论)-夜的第一章
mongoDB的使用
C3D模型pytorch源码逐句详析(一)
广度优先遍历(图和二叉树的层序遍历相关问题)
安装mysql时,string the service 安装失败>mysql80启动失败
Common methods of JS digital thousand bit segmentation
Pytorch 通过 Tensor 某一维的值将 Tensor 分开的方法(简易)
多线程——静态代理模式
常用类的小知识
Leetcode 560 前缀和+哈希表
修改mysql的分组报错Expression #1 of SELECT list is not in GROUP
Detailed explanation of JDBC operation database
Exception handling exception
基础背包问题
Basic knapsack problem
多线程——死锁和synchronized
Salt FAQs
Angr(九)——angr_ctf
message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“
PyTorch 对 Batch 中每个样本计算损失 Loss for each sample