# 581. Shortest Unsorted Continuous Subarray # 769. Max Chunks To Make Sorted

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

  参考了讨论区的大神思路:先定义两个标签索引变量begin = -1 与 end = -2(选择-1与-2这两个数是为了当数组中不存在非降序子列时返回 end - begin + 1==0),接着同时开始从头到尾(定义变量max表示搜索到的最大值)以及从尾到头(定义变量min表示搜索到的最小值)两个方向的搜索:
①从头到尾:当当前值比max小时(后值比前值小,不是升序),说明无序子列的end至少在当前位置。
②从尾到头:当当前值比min大时(前值比后值大,不是升序),说明无序子列的begin至少要从该位置开始

class Solution {
public:
    //参考了讨论区的最高评论答案
    int findUnsortedSubarray(vector<int>& nums) {
        int begin = -1, end = -2;
        int max = nums[0], min = nums[nums.size()-1];
        int n = nums.size();
        for(int i = 1; i<nums.size(); i++)   //向右从1到n-1,向左从n-2到0
        {
            
            if(nums[i] < max)
                end = i;
            if(nums[n-1-i] > min)
                begin = n-1-i;
            max = nums[i]>max ? nums[i]:max;
            min = nums[n-1-i]<min ? nums[n-1-i]:min;
        }
        return end - begin + 1;
    }
};

769. Max Chunks To Make Sorted

Given an array arr that is a permutation of [0, 1, …, arr.length - 1], we split the array into some number of “chunks” (partitions), and individually sort each chunk. After concatenating them, the result equals the sorted array.

What is the most number of chunks we could have made?

Example 1:

Input: arr = [4,3,2,1,0]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn’t sorted.

Example 2:

Input: arr = [1,0,2,3,4]
Output: 4
Explanation:
We can split into two chunks, such as [1, 0], [2, 3, 4].
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.

Note:

arr will have length in range [1, 10].
arr[i] will be a permutation of [0, 1, …, arr.length - 1].

设置一个记录最大值的int变量,在数组中从左向右搜索,当当前搜索的最大值与当前索引相同时,分块加1。注意,当当前最大值等于当前索引时,数组中在当前索引下的值分两种情况:
①:如果当前最大值大于数组在当前索引下的值,则在后续的搜索过程中不会有比当前值更大的值了(因为数组中的数是从0到size-1的,如果排好序应该是[0,1,2…size-1])此时分块+1.
②如果当前最大值小于数组在当前索引下的值,说明在该索引之后有比当前最大值小的值需要排序,如果此时形成分块,无法包括这个小值,导致最后整个数组无法正常排序

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int maxnum = 0;
        int chunksnum = 0;
        for(int i = 0; i<arr.size(); i++)
        {
            maxnum = max(maxnum, arr[i]);
            if(maxnum == i)
            {
                chunksnum++;
            }
        }
        return chunksnum;
    }
};

tip:
这两题都是在一个数组中找无序数列排序使得整个数组有序,注意他们各自寻找无序子列的方法。
合理设置标签指针,对数组同时进行正序与倒序的搜索

猜你喜欢

转载自blog.csdn.net/qq_39743607/article/details/83621334