Algorithm


Problem Name: 769. Max Chunks To Make Sorted

You are given an integer array arr of length n that represents a permutation of the integers in the range [0, n - 1].

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.

 

Example 1:

Input: arr = [4,3,2,1,0]
Output: 1
Explanation:
Splitting into two or more chunks will not return the required result.
For example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted.

Example 2:

Input: arr = [1,0,2,3,4]
Output: 4
Explanation:
We can split into two chunks, such as [1, 0], [2, 3, 4].
However, splitting into [1, 0], [2], [3], [4] is the highest number of chunks possible.

 

Constraints:

  • n == arr.length
  • 1 <= n <= 10
  • 0 <= arr[i] < n
  • All the elements of arr are unique.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int count = 0, maxDis = 0;
        for(int i = 0; i  <  arr.size(); i++){
            maxDis = max(maxDis, arr[i]);
            if(maxDis == i) count++;
        }
        return count;
    }
};

class Solution {
public:
    int maxChunksToSorted(vector<int>& arr) {
        int curMax = 0, n = arr.size(), res = 0;
        for (int i = 0; i < n; ++i) {
            curMax = max(curMax, arr[i]);
            if (curMax == i> {
                ++res;
            }
        }
        return res;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [4,3,2,1,0]

Output

x
+
cmd
1

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
    public int maxChunksToSorted(int[] arr) {
        int maxSplitCount = 0;
        int maxVal = 0;
        int idx = 0;
        
        while (idx  <  arr.length) {
            maxVal = Math.max(maxVal, arr[idx]);
            if (maxVal == idx) {
                maxSplitCount++;
            }
            
            idx++;
        }
        
        return maxSplitCount;
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [4,3,2,1,0]

Output

x
+
cmd
1

#3 Code Example with Javascript Programming

Code - Javascript Programming


const maxChunksToSorted = function(arr) {
  let res = 0;
  let max = 0;
  for (let i = 0; i  <  arr.length; i++) {
    max = Math.max(max, arr[i]);
    if (max === i) res += 1;
  }
  return res;
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [1,0,2,3,4]

Output

x
+
cmd
4

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def maxChunksToSorted(self, arr):
        """
        :type arr: List[int]
        :rtype: int
        """
        max_seen, total_seen, res_count = 0, 0, 0
        for num in arr:
            max_seen = max(max_seen, num)
            total_seen += 1
            if max_seen == total_seen - 1:
                res_count += 1
        return res_count
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [1,0,2,3,4]

Output

x
+
cmd
4
Advertisements

Demonstration


Previous
#768 Leetcode Max Chunks To Make Sorted II Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#770 Leetcode Basic Calculator IV Solution in C, C++, Java, JavaScript, Python, C# Leetcode