Class Solution
-
- All Implemented Interfaces:
public final class Solution3080 - Mark Elements on Array by Performing Queries\.
Medium
You are given a 0-indexed array
numsof sizenconsisting of positive integers.You are also given a 2D array
queriesof sizemwhere <code>queriesi = index<sub>i</sub>, k<sub>i</sub></code>.Initially all elements of the array are unmarked.
You need to apply
mqueries on the array in order, where on the <code>i<sup>th</sup></code> query you do the following:Mark the element at index <code>index<sub>i</sub></code> if it is not already marked.
Then mark <code>k<sub>i</sub></code> unmarked elements in the array with the smallest values. If multiple such elements exist, mark the ones with the smallest indices. And if less than <code>k<sub>i</sub></code> unmarked elements exist, then mark all of them.
Return an array answer of size
mwhereanswer[i]is the sum of unmarked elements in the array after the <code>i<sup>th</sup></code> query.Example 1:
Input: nums = 1,2,2,1,2,3,1, queries = \[\[1,2],3,3,4,2]
Output: 8,3,0
Explanation:
We do the following queries on the array:
Mark the element at index
1, and2of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are <code>nums = **<ins>1</ins>** ,<ins> **2** </ins>,2,<ins> **1** </ins>,2,3,1</code>. The sum of unmarked elements is2 + 2 + 3 + 1 = 8.Mark the element at index
3, since it is already marked we skip it. Then we mark3of the smallest unmarked elements with the smallest indices, the marked elements now are <code>nums = **<ins>1</ins>** ,<ins> **2** </ins>,<ins> **2** </ins>,<ins> **1** </ins>,<ins> **2** </ins>,3, **<ins>1</ins>** </code>. The sum of unmarked elements is3.Mark the element at index
4, since it is already marked we skip it. Then we mark2of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are <code>nums = **<ins>1</ins>** ,<ins> **2** </ins>,<ins> **2** </ins>,<ins> **1** </ins>,<ins> **2** </ins>, **<ins>3</ins>** ,<ins> **1** </ins></code>. The sum of unmarked elements is0.
Example 2:
Input: nums = 1,4,2,3, queries = \[\[0,1]]
Output: 7
Explanation: We do one query which is mark the element at index
0and mark the smallest element among unmarked elements. The marked elements will be <code>nums = **<ins>1</ins>** ,4,<ins> **2** </ins>,3</code>, and the sum of unmarked elements is4 + 3 = 7.Constraints:
n == nums.lengthm == queries.length<code>1 <= m <= n <= 10<sup>5</sup></code>
<code>1 <= numsi<= 10<sup>5</sup></code>
queries[i].length == 2<code>0 <= index<sub>i</sub>, k<sub>i</sub><= n - 1</code>