LeetCode – 768. Max Chunks To Make Sorted II

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

题意:

给一个排列,可能有重复元素,我们将数组拆分成一些“块”(分区),并对每个块进行单独排序。连接它们之后,结果等于排序后的数组。问最多能够分成多少个分区(块)

分析:

这道题目的思路和LeetCode – 769. Max Chunks To Make Sorted一样,关键是找出划分合理chunk的条件。

因为有重复元素,可以考虑判断累加和的方式,排序后的数组前i个元素累加的和等于原数组前i个数累加的和时可以分为一个块~

具体实现如下:

public int name(int[] arr) {
	int sum1 = 0, sum2 = 0, ans = 0;
	int[] t = new int[arr.length];
	System.arraycopy(arr, 0, t, 0, arr.length);;
	Arrays.sort(t);
	for(int i = 0; i < arr.length; i++) {
		sum1 += t[i];
		sum2 += arr[i];
		if(sum1 == sum2) ans++;
	}
	return ans;
}

你可能感兴趣的:(数据结构,leetcode,Max,Chunks,To,Make,Sorted,II)