Problem statement

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

Solution

Just see solution for problem 0768 Max Chunks To Make Sorted, this problem is special case of it.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def maxChunksToSorted(self, arr):
        left_max = list(accumulate(arr, max))[:-1]
        right_min = list(accumulate(arr[::-1], min))[::-1][1:]
        return sum(i <= j for i,j in zip(left_max, right_min)) + 1