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?
|
|
|
|
题目大意
将arr拆分n个部分,使得每个部分分别排序之后,重新组合在一起的数组呈递增序列。
解题思路
使数组呈递减序列,不满足条件的既加1:
arr[i..<arr.count].min()! > arr[0..<i].max()!
代码
|
|
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?
|
|
|
|
题目大意
这是I
的变形,数组可能有重复元素。
解题思路
通过当前数组记为
arr
和已排序数组记为sortArr
进行对比,如果之前的元素个数不相等则计数器加一。
只有当计数器为0,即代表arr和sortArr前N项元素相同,结果加一。
代码
|
|