2718. Sum of Matrix After Queries LeetCode Solution

In this guide, you will get 2718. Sum of Matrix After Queries LeetCode Solution with the best time and space complexity. The solution to Sum of Matrix After Queries problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sum of Matrix After Queries solution in C++
  4. Sum of Matrix After Queries solution in Java
  5. Sum of Matrix After Queries solution in Python
  6. Additional Resources
2718. Sum of Matrix After Queries LeetCode Solution image

Problem Statement of Sum of Matrix After Queries

You are given an integer n and a 0-indexed 2D array queries where queries[i] = [typei, indexi, vali].
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:

if typei == 0, set the values in the row with indexi to vali, overwriting any previous values.
if typei == 1, set the values in the column with indexi to vali, overwriting any previous values.

Return the sum of integers in the matrix after all queries are applied.

Example 1:

Input: n = 3, queries = [[0,0,1],[1,2,2],[0,2,3],[1,0,4]]
Output: 23
Explanation: The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 23.

Example 2:

Input: n = 3, queries = [[0,0,4],[0,1,2],[1,0,1],[0,2,3],[1,2,1]]
Output: 17
Explanation: The image above describes the matrix after each query. The sum of the matrix after all queries are applied is 17.

Constraints:

1 <= n <= 104
1 <= queries.length <= 5 * 104
queries[i].length == 3
0 <= typei <= 1
0 <= indexi < n
0 <= vali <= 105

Complexity Analysis

  • Time Complexity: O(n + q)
  • Space Complexity: O(n)

2718. Sum of Matrix After Queries LeetCode Solution in C++

class Solution {
 public:
  long long matrixSumQueries(int n, vector<vector<int>>& queries) {
    long ans = 0;
    // seen[0] := row, seen[1] := col
    vector<vector<bool>> seen(2, vector<bool>(n));
    // notSet[0] = row, notSet[1] := col
    vector<int> notSet(2, n);

    // Later queries dominate.
    for (int i = queries.size() - 1; i >= 0; --i) {
      const int type = queries[i][0];
      const int index = queries[i][1];
      const int val = queries[i][2];
      if (!seen[type][index]) {
        ans += val * notSet[type ^ 1];
        seen[type][index] = true;
        --notSet[type];
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

2718. Sum of Matrix After Queries LeetCode Solution in Java

class Solution {
  public long matrixSumQueries(int n, int[][] queries) {
    long ans = 0;
    // seen[0] := row, seen[1] := col
    boolean[][] seen = new boolean[2][n];
    // notSet[0] = row, notSet[1] := col
    int[] notSet = new int[2];
    Arrays.fill(notSet, n);

    // Late queries dominate.
    for (int i = queries.length - 1; i >= 0; --i) {
      final int type = queries[i][0];
      final int index = queries[i][1];
      final int val = queries[i][2];
      if (!seen[type][index]) {
        ans += val * notSet[type ^ 1];
        seen[type][index] = true;
        --notSet[type];
      }
    }

    return ans;
  }
}
// code provided by PROGIEZ

2718. Sum of Matrix After Queries LeetCode Solution in Python

class Solution:
  def matrixSumQueries(self, n: int, queries: list[list[int]]) -> int:
    ans = 0
    # seen[0] := row, seen[1] := col
    seen = [[False] * n for _ in range(2)]
    # notSet[0] = row, notSet[1] := col
    notSet = [n] * 2

    # Late queries dominate.
    for type, index, val in reversed(queries):
      if not seen[type][index]:
        ans += val * notSet[type ^ 1]
        seen[type][index] = True
        notSet[type] -= 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.