【CODE】Max Chunks To Make Sorted

769. Max Chunks To Make Sorted

Medium

55592FavoriteShare

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].
class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int maxx=0,res=0;
        vector<int> tmp1,tmp2;
        for(int i=0;i<arr.size();i++){
            tmp1.push_back(arr[i]);
            tmp2.push_back(i);
            sort(tmp1.begin(),tmp1.end());
            if(tmp1==tmp2){
                res++;
                tmp1.clear();
                tmp2.clear();
            }
        }
        return res;
    }
};
  • Runtime: 4 ms, faster than 54.96% of C++ online submissions for Max Chunks To Make Sorted.
  • Memory Usage: 8.2 MB, less than 100.00% of C++ online submissions for Max Chunks To Make Sorted.
  • Next challenges: 
  • Max Chunks To Make Sorted II
class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int maxx=0,res=0;
        for(int i=0;i<arr.size();i++){
            maxx=max(maxx,arr[i]);
            if(maxx==i) res++;
        }
        return res;
    }
};

768. Max Chunks To Make Sorted II

Hard

30512FavoriteShare

This question is the same as "Max Chunks to Make Sorted" except the integers of the given array are not necessarily distinct, the input array could be up to length 2000, and the elements could be up to 10**8.


Given an array arr of integers (not necessarily distinct), 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 = [5,4,3,2,1]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.

Example 2:

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

Note:

  • arr will have length in range [1, 2000].
  • arr[i] will be an integer in range [0, 10**8].

(思路借鉴:https://www.cnblogs.com/grandyang/p/8850299.html

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int res=0;
        long long int sum1=0,sum2=0;
        vector<int> tmp1=arr;
        sort(tmp1.begin(),tmp1.end());
        for(int i=0;i<arr.size();i++){
            sum1+=tmp1[i];
            sum2+=arr[i];
            if(sum1==sum2){
                res++;
                sum1=0;
                sum2=0;
            }
        }
        return res;
    }
};
class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int res=1,maxx=0,minn=0;
        for(int i=0;i<arr.size()-1;i++){
            maxx=*max_element(arr.begin(),arr.begin()+i+1);
            minn=*min_element(arr.begin()+1+i,arr.end());
            if(maxx<=minn) res++;
        }
        return res;
    }
};
class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int res=1,maxx=0;
        vector<int> minn=arr;
        for(int i=arr.size()-2;i>=0;i--) minn[i]=min(arr[i],minn[i+1]);
        for(int i=0;i<arr.size()-1;i++){
            if(maxx<arr[i]) maxx=arr[i];
            if(maxx<=minn[i+1]) res++;
        }
        return res;
    }
};
  • 方法二:后面块的最小值≥前面块的最大值
  • Runtime: 208 ms, faster than 5.19% of C++ online submissions for Max Chunks To Make Sorted II.
  • Memory Usage: 8.9 MB, less than 100.00% of C++ online submissions for Max Chunks To Make Sorted II.
  • Runtime: 8 ms, faster than 99.16% of C++ online submissions for Max Chunks To Make Sorted II.
  • Memory Usage: 9.1 MB, less than 100.00% of C++ online submissions for Max Chunks To Make Sorted II.
  • 上述博客中提到单调递增栈的方法,没怎么看懂。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值