Skip to content

Commit

Permalink
Create max-chunks-to-make-sorted-ver-2.py
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jan 21, 2018
1 parent ce393bf commit 24bea72
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Python/max-chunks-to-make-sorted-ver-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Time: O(nlogn)
# Space: O(n)

class Solution(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

0 comments on commit 24bea72

Please sign in to comment.