Skip to content

Commit 6e5b82b

Browse files
authored
Create earliest-second-to-mark-indices-i.py
1 parent e892214 commit 6e5b82b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Time: O(mlogm)
2+
# Space: O(n)
3+
4+
# binary search, greedy
5+
class Solution(object):
6+
def earliestSecondToMarkIndices(self, nums, changeIndices):
7+
"""
8+
:type nums: List[int]
9+
:type changeIndices: List[int]
10+
:rtype: int
11+
"""
12+
def check(t):
13+
lookup = [-1]*len(nums)
14+
for i in xrange(t):
15+
lookup[changeIndices[i]-1] = i
16+
if -1 in lookup:
17+
return False
18+
cnt = 0
19+
for i in xrange(t):
20+
if i != lookup[changeIndices[i]-1]:
21+
cnt += 1
22+
continue
23+
cnt -= nums[changeIndices[i]-1]
24+
if cnt < 0:
25+
return False
26+
return True
27+
28+
left, right = sum(nums)+len(nums), len(changeIndices)
29+
while left <= right:
30+
mid = left+(right-left)//2
31+
if check(mid):
32+
right = mid-1
33+
else:
34+
left = mid+1
35+
return left if left <= len(changeIndices) else -1

0 commit comments

Comments
 (0)