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].
题目大意:给一个排列,可能有重复元素,我们将数组拆分成一些“块”(分区),并对每个块进行单独排序。连接它们之后,结果等于排序后的数组。问最多能够分成多少个分区(块)
分析:因为有重复元素,可以考虑判断累加和的方式,排序后的数组前i个元素累加的和等于原数组前i个数累加的和时可以分为一个块~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Solution { public: int maxChunksToSorted(vector<int>& arr) { int sum1 = 0, sum2 = 0, ans = 0; vector<int> t = arr; sort(t.begin(), t.end()); for(int i = 0; i < arr.size(); i++) { sum1 += t[i]; sum2 += arr[i]; if(sum1 == sum2) ans++; } return ans; } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼