3080. Mark Elements on Array by Performing Queries Medium

@problem@discussion
#Array#Hash Table#Sorting#Heap (Priority Queue)#Simulation



1/**
2 * [3080] Mark Elements on Array by Performing Queries
3 *
4 * You are given a 0-indexed array nums of size n consisting of positive integers.
5 * You are also given a 2D array queries of size m where queries[i] = [indexi, ki].
6 * Initially all elements of the array are unmarked.
7 * You need to apply m queries on the array in order, where on the i^th query you do the following:
8 * 
9 * 	Mark the element at index indexi if it is not already marked.
10 * 	Then mark ki 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 ki unmarked elements exist, then mark all of them.
11 * 
12 * Return an array answer of size m where answer[i] is the sum of unmarked elements in the array after the i^th query.
13 *  
14 * <strong class="example">Example 1:
15 * <div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
16 * Input: <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,2,2,1,2,3,1], queries = [[1,2],[3,3],[4,2]]</span>
17 * Output: <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">[8,3,0]</span>
18 * Explanation:
19 * We do the following queries on the array:
20 * 
21 * 	Mark the element at index 1, and 2 of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are nums = [<u>1</u>,<u>2</u>,2,<u>1</u>,2,3,1]. The sum of unmarked elements is 2 + 2 + 3 + 1 = 8.
22 * 	Mark the element at index 3, since it is already marked we skip it. Then we mark 3 of the smallest unmarked elements with the smallest indices, the marked elements now are nums = [<u>1</u>,<u>2</u>,<u>2</u>,<u>1</u>,<u>2</u>,3,<u>1</u>]. The sum of unmarked elements is 3.
23 * 	Mark the element at index 4, since it is already marked we skip it. Then we mark 2 of the smallest unmarked elements with the smallest indices if they exist, the marked elements now are nums = [<u>1</u>,<u>2</u>,<u>2</u>,<u>1</u>,<u>2</u>,<u>3</u>,<u>1</u>]. The sum of unmarked elements is 0.
24 * </div>
25 * <strong class="example">Example 2:
26 * <div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
27 * Input: <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">nums = [1,4,2,3], queries = [[0,1]]</span>
28 * Output: <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">[7]</span>
29 * Explanation:  We do one query which is mark the element at index 0 and mark the smallest element among unmarked elements. The marked elements will be nums = [<u>1</u>,4,<u>2</u>,3], and the sum of unmarked elements is 4 + 3 = 7.
30 * </div>
31 *  
32 * Constraints:
33 * 
34 * 	n == nums.length
35 * 	m == queries.length
36 * 	1 <= m <= n <= 10^5
37 * 	1 <= nums[i] <= 10^5
38 * 	queries[i].length == 2
39 * 	0 <= indexi, ki <= n - 1
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/mark-elements-on-array-by-performing-queries/
45// discuss: https://leetcode.com/problems/mark-elements-on-array-by-performing-queries/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn unmarked_sum_array(nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i64> {
51        
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_3080() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.