Skip to content

Commit b69b5d8

Browse files
authored
Create mark-elements-on-array-by-performing-queries.py
1 parent d7e5de1 commit b69b5d8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Time: O(q + nlogn)
2+
# Space: O(n)
3+
4+
# hash table, heap
5+
class Solution(object):
6+
def unmarkedSumArray(self, nums, queries):
7+
"""
8+
:type nums: List[int]
9+
:type queries: List[List[int]]
10+
:rtype: List[int]
11+
"""
12+
total = sum(nums)
13+
lookup = [False]*len(nums)
14+
min_heap = [(x, i) for i, x in enumerate(nums)]
15+
heapq.heapify(min_heap)
16+
result = []
17+
for i, k in queries:
18+
if not lookup[i]:
19+
lookup[i] = True
20+
total -= nums[i]
21+
for _ in xrange(k):
22+
while min_heap:
23+
x, i = heapq.heappop(min_heap)
24+
if lookup[i]:
25+
continue
26+
lookup[i] = True
27+
total -= x
28+
break
29+
if not min_heap:
30+
break
31+
result.append(total)
32+
return result

0 commit comments

Comments
 (0)