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:
typei == 0, set the values in the row with indexi to vali, overwriting any previous values.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 <= 1041 <= queries.length <= 5 * 104queries[i].length == 30 <= typei <= 10 <= indexi < n0 <= vali <= 105Instead of updating a full matrix, which could be inefficient considering the constraints, we simulate the updates using arrays to keep track of row and column updates. This avoids the need to modify the entire matrix, thus saving computation time and space.
This solution uses two arrays, one for row updates and another for column updates, to store the latest query operation on each row and column. The arrays row_updates and col_updates record the index of the last query that affected each row or column. The computation of the final matrix sum happens by choosing the latest applicable query update for each cell.
C
Time Complexity: O(n^2) - We iterate over all cells to compute the final sum.
Space Complexity: O(n) - We use auxiliary arrays for row and column update indices and values.
By processing the queries in reverse order, we can efficiently manage the rows and columns to prevent unnecessary overwrites. By using flags to record rows and columns that have already been set, we can avoid redundant operations and achieve an optimized simulation.
This JavaScript solution processes queries in reverse to track which rows and columns have been set. By using boolean arrays to mark applied operations, we accumulate sums only for the first (in reverse order) applicable update, thus minimizing redundant calculations.
C++
Time Complexity: O(q + n) - We iterate over queries and use constant space checks.
Space Complexity: O(n) - We use flag arrays for row and column operations.
| Approach | Complexity |
|---|---|
| Direct Simulation with Efficient Storage | Time Complexity: O(n^2) - We iterate over all cells to compute the final sum. |
| Optimized Reverse Simulation | Time Complexity: O(q + n) - We iterate over queries and use constant space checks. |
LeetCode is a JOKE with This ONE WEIRD TRICK • AlgoMonster • 110,978 views views
Watch 9 more video solutions →Practice Sum of Matrix After Queries with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor