Leetcode 780 Solution

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

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

Solution

class Solution:
    def maxChunksToSorted(self, arr: List[int]) -> int:
        ans = 0

        m = collections.defaultdict(int)

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

            if m[a] == 0:
                m.pop(a)
            if m[i] == 0:
                m.pop(i)

            if len(m) == 0:
                ans += 1
        return ans