题目地址
LeetCode#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?
Example 1:
1 | Input: arr = [4,3,2,1,0] |
Example 2:
1 | Input: arr = [1,0,2,3,4] |
Note:
arr
will have length in range[1, 10]
.arr[i]
will be a permutation of[0, 1, ..., arr.length - 1]
.
解题思路
这道题题意是将一个无序数组排列分割最大次使得每一次分割的子数组排序后再拼接起来使整个数组有序。
索引遍历数组,以索引为分割点,索引左部(包括本身)的最大值如果小于右部的最小值,说明这两个不在一个分组内,给res加一。
解题代码【.CPP】
1 | class Solution { |