2718. Sum of Matrix After Queries Medium
1/**
2 * [2718] Sum of Matrix After Queries
3 *
4 * You are given an integer n and a 0-indexed 2D array queries where queries[i] = [typei, indexi, vali].
5 * Initially, there is a 0-indexed n x n matrix filled with 0's. For each query, you must apply one of the following changes:
6 *
7 * if typei == 0, set the values in the row with indexi to vali, overwriting any previous values.
8 * if typei == 1, set the values in the column with indexi to vali, overwriting any previous values.
9 *
10 * Return the sum of integers in the matrix after all queries are applied.
11 *
12 * <strong class="example">Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2023/05/11/exm1.png" style="width: 681px; height: 161px;" />
14 * Input: n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]
15 * Output: 23
16 * Explanation: The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 23.
17 *
18 * <strong class="example">Example 2:
19 * <img alt="" src="https://assets.leetcode.com/uploads/2023/05/11/exm2.png" style="width: 681px; height: 331px;" />
20 * Input: n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]
21 * Output: 17
22 * Explanation: The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 17.
23 *
24 *
25 * Constraints:
26 *
27 * 1 <= n <= 10^4
28 * 1 <= queries.length <= 5 * 10^4
29 * queries[i].length == 3
30 * 0 <= typei <= 1
31 * 0 <= indexi < n
32 * 0 <= vali <= 10^5
33 *
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/sum-of-matrix-after-queries/
38// discuss: https://leetcode.com/problems/sum-of-matrix-after-queries/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43 pub fn matrix_sum_queries(n: i32, queries: Vec<Vec<i32>>) -> i64 {
44
45 }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_2718() {
56 }
57}
58
Back
© 2025 bowen.ge All Rights Reserved.