-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
earliest-second-to-mark-indices-ii.py
42 lines (38 loc) · 1.32 KB
/
earliest-second-to-mark-indices-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
38
39
40
41
42
# Time: O((m + nlogn) * logm)
# Space: O(n)
import heapq
# binary search, greedy, heap
class Solution(object):
def earliestSecondToMarkIndices(self, nums, changeIndices):
"""
:type nums: List[int]
:type changeIndices: List[int]
:rtype: int
"""
def check(t):
min_heap = []
cnt = 0
for i in reversed(xrange(t)):
if i != lookup[changeIndices[i]-1]:
cnt += 1
continue
heapq.heappush(min_heap, nums[changeIndices[i]-1])
if cnt:
cnt -= 1
else:
cnt += 1
heapq.heappop(min_heap)
return total-(sum(min_heap)+len(min_heap)) <= cnt
lookup = [-1]*len(nums)
for i in reversed(xrange(len(changeIndices))):
if nums[changeIndices[i]-1]:
lookup[changeIndices[i]-1] = i
total = sum(nums)+len(nums)
left, right = sum((1 if lookup[i] != -1 else nums[i]) for i in xrange(len(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