zoukankan      html  css  js  c++  java
  • 581. Shortest Unsorted Continuous Subarray

    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 <=.

    数组的题常用方法 :排序、最大值指针边走边找、最小值指针边走边找,数组的其他指针位置—顺序遍历,

    逆序遍历,其他指针与最大值指针比较的后果或最小值指针

    先想排序能否做:

    public class Solution {
        public int findUnsortedSubarray(int[] nums) {
            int[] snums = nums.clone();
            Arrays.sort(snums);
            int start = snums.length, end = 0;
            for (int i = 0; i < snums.length; i++) {
                if (snums[i] != nums[i]) {
                    start = Math.min(start, i);
                    end = Math.max(end, i);
                }
            }
            return (end - start >= 0 ? end - start + 1 : 0);
        }
    }
    

    Stack 画图: 利用相对位置和大小pop找到最左边的位置, 和最右边的位置, 利用栈内的元素都是最小的特点, 找到要排序的sub 中最小的点应该在的排序好后的位置; 利用栈内的元素都是最大的特点, 找到要排序的sub 中最大的点应该在的排序好后的位置; 此位置即是sub数组的要排序的起点和终点

    会画图找位置

    In order to determine the correct position of nums[j]nums[j],

    we keep on popping the elemnents from the top of the stackstack until we reach the stage where the element(corresponding to the index)

    on the top of the stackstack is lesser than nums[j]nums[j]. 

    public class Solution {
        public int findUnsortedSubarray(int[] nums) {
            Stack < Integer > stack = new Stack < Integer > ();
            int l = nums.length, r = 0;
            for (int i = 0; i < nums.length; i++) {
                while (!stack.isEmpty() && nums[stack.peek()] > nums[i])
                    l = Math.min(l, stack.pop());
                stack.push(i);
            }
            stack.clear();
            for (int i = nums.length - 1; i >= 0; i--) {
                while (!stack.isEmpty() && nums[stack.peek()] < nums[i])
                    r = Math.max(r, stack.pop());
                stack.push(i);
            }
            return r - l > 0 ? r - l + 1 : 0;
        }
    }
    

     

    Complexity Analysis

    • Time complexity : O(n)O(n). Stack of size nn is filled.

    • Space complexity : O(n)O(n). Stack size grows upto nn.

    因为只需要记坐标和最大值最小值,  可将上述的栈空间优化, 但是对遍历的顺序有要求-- 还是 找sub 里最大点应该在的最右的位置顺序-- 栈是因为可以回退到右边的, 最小点应该在的最左边的位置---栈可以回退的; 因为最值都可找到, 但是最左最右是对遍历顺序有要求的

    public int findUnsortedSubarray(int[] nums) {
            int n = nums.length;
            if (n == 0 || nums == null) {
                return 0;
            }
            int max = nums[0], min = nums[n - 1], beg = - 2, end = - 1;
            for (int i = 1; i < n; i++) {
                max = Math.max(max, nums[i]);
                min = Math.min(min, nums[n - 1 - i]);
                if (nums[i] < max) end = i;
                if (nums[n - 1 - i] > min) beg = n - 1- i;
            }
            return end - beg + 1;
    }
    

      

  • 相关阅读:
    Postgresql10离线安装
    Clickhouse集群安装部署
    Clickhouse建表语法、视图语法、数据表DDL(数据定义语言)、数据DML(数据操作语言)
    Clickhouse基础语法、数据类型、数据表引擎学习
    Spring4.0+Mybatis整合时占位符无法读取jdbc.properties的问题
    Code: 210. DB::NetException: Connection refused (localhost:9000)
    使用Jdbc的方式连接Clickhouse列式数据库
    Dbeaver连接不上远程服务器部署的Clickhouse问题
    Clickhouse入门学习、单机、集群安装部署
    Another Redis DeskTop Manage一款免费的Redis可视化工具
  • 原文地址:https://www.cnblogs.com/apanda009/p/7296289.html
Copyright © 2011-2022 走看看