-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathearliest-second-to-mark-indices-i.py
More file actions
35 lines (33 loc) · 1.02 KB
/
earliest-second-to-mark-indices-i.py
File metadata and controls
35 lines (33 loc) · 1.02 KB
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
# Time: O(mlogm)
# Space: O(n)
# binary search, greedy
class Solution(object):
def earliestSecondToMarkIndices(self, nums, changeIndices):
"""
:type nums: List[int]
:type changeIndices: List[int]
:rtype: int
"""
def check(t):
lookup = [-1]*len(nums)
for i in xrange(t):
lookup[changeIndices[i]-1] = i
if -1 in lookup:
return False
cnt = 0
for i in xrange(t):
if i != lookup[changeIndices[i]-1]:
cnt += 1
continue
cnt -= nums[changeIndices[i]-1]
if cnt < 0:
return False
return True
left, right = sum(nums)+len(nums), len(changeIndices)
while left <= right:
mid = left+(right-left)//2
if check(mid):
right = mid-1
else:
left = mid+1
return left if left <= len(changeIndices) else -1