768. Max Chunks To Make Sorted II
Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 54% Topics: Array, Stack, Greedy, Sorting, Monotonic Stack
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(n)
# mono stack solution
class Solution(object):
def maxChunksToSorted(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
result, increasing_stk = 0, []
for num in arr:
max_num = num if not increasing_stk else max(increasing_stk[-1], num)
while increasing_stk and increasing_stk[-1] > num:
increasing_stk.pop()
increasing_stk.append(max_num)
return len(increasing_stk)
# Time: O(nlogn)
# Space: O(n)
class Solution2(object):
def maxChunksToSorted(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
def compare(i1, i2):
return arr[i1]-arr[i2] if arr[i1] != arr[i2] else i1-i2
idxs = [i for i in xrange(len(arr))]
result, max_i = 0, 0
for i, v in enumerate(sorted(idxs, cmp=compare)):
max_i = max(max_i, v)
if max_i == i:
result += 1
return result
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions