forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
max-chunks-to-make-sorted-ii.py
38 lines (33 loc) · 1021 Bytes
/
max-chunks-to-make-sorted-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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