3306. Mark Elements on Array by Performing Queries
My accepted Python solution to LeetCode problem 3306, Mark Elements on Array by Performing Queries, running in 1934ms.
- Difficulty: Medium
- Python
- Runtime 1934ms
- Memory 48.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 1934ms, memory 48.3MB, accepted 2025-12-29.
class Solution:
def unmarkedSumArray(self, nums: List[int], queries: List[List[int]]) -> List[int]:
import heapq
n = len(nums)
marked = [False] * n
total_sum = sum(nums)
# Min heap: (value, index)
heap = [(nums[i], i) for i in range(n)]
heapq.heapify(heap)
result = []
for idx, k in queries:
# Mark element at idx if not already marked
if not marked[idx]:
marked[idx] = True
total_sum -= nums[idx]
# Mark k smallest unmarked elements
count = 0
while heap and count < k:
val, i = heapq.heappop(heap)
if not marked[i]:
marked[i] = True
total_sum -= val
count += 1
result.append(total_sum)
return result