Leetcode 779 Solution

This article provides solution to leetcode question 779 (max-chunks-to-make-sorted-ii)

https://leetcode.com/problems/max-chunks-to-make-sorted-ii

Solution

class Solution:
    def maxChunksToSorted(self, arr: List[int]) -> int:
        m = collections.defaultdict(int)

        counted = []
        for i, a in enumerate(arr):
            m[a] += 1
            counted.append((a, m[a]))

        cur = (0, 0)
        ans = 0
        for x, y in zip(counted, sorted(counted)):
            cur = max(x, cur)
            if cur == y:
                ans += 1
                cur = (0, 0)
        return ans