You are given an integer array arr
.
We split arr
into some number of chunks (i.e., partitions), and individually sort each chunk. After concatenating them, the result should equal the sorted array.
Return the largest number of chunks we can make to sort the array.
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.
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.
1 <= arr.length <= 2000
0 <= arr[i] <= 108
use std::collections::BinaryHeap;
use std::collections::HashMap;
impl Solution {
pub fn max_chunks_to_sorted(arr: Vec<i32>) -> i32 {
let mut heap = arr.iter().collect::<BinaryHeap<_>>();
let mut pop_count = HashMap::new();
let mut min_num = i32::MAX;
let mut ret = 0;
for i in (0..arr.len()).rev() {
*pop_count.entry(arr[i]).or_insert(0) += 1;
min_num = min_num.min(arr[i]);
while *pop_count.get(*heap.peek().unwrap_or(&&-1)).unwrap_or(&0) > 0 {
*pop_count.get_mut(heap.pop().unwrap()).unwrap() -= 1;
}
if **heap.peek().unwrap_or(&&-1) <= min_num {
ret += 1;
}
}
ret
}
}