Skip to content

Commit 8c90abe

Browse files
authored
Create minimum-unlocked-indices-to-sort-nums.py
1 parent 9d602f7 commit 8c90abe

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# sort
5+
class Solution(object):
6+
def minUnlockedIndices(self, nums, locked):
7+
"""
8+
:type nums: List[int]
9+
:type locked: List[int]
10+
:rtype: int
11+
"""
12+
result = mx = cnt = 0
13+
for i in xrange(len(nums)):
14+
if mx < nums[i]:
15+
mx = nums[i]
16+
cnt = 0
17+
elif mx > nums[i]:
18+
if mx != nums[i]+1:
19+
return -1
20+
result += cnt
21+
cnt = 0
22+
cnt += locked[i]
23+
return result

0 commit comments

Comments
 (0)