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;
}
};
- 方法一:利用和的思想。
- Runtime: 16 ms, faster than 44.60% 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.
- Next challenges:
- Find Minimum in Rotated Sorted Array
- Shortest Word Distance III
- Max Area of Island
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.
- 上述博客中提到单调递增栈的方法,没怎么看懂。