Sum of Matrix After Queries - Complete Solution Guide
Sum of Matrix After Queries is LeetCode problem 2718, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer n and a 0-indexed 2D array queries where queries[i] = [type i , index i , val i ] . 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 type i == 0 , set the values in the row with index i to val i , overwriting any previous values. if type i == 1 , set the values in the column with index i to val i , overwriting any previous values. Return the sum of integers in the matrix after all queries are a
Detailed Explanation
The problem presents an n x n matrix initially filled with zeros. We are given a series of queries. Each query instructs us to either set all values in a specific row to a given value or set all values in a specific column to a given value. The task is to determine the sum of all elements in the matrix after applying all queries.
Solution Approach
The solution processes the queries in reverse order. This approach is crucial because the last query that modifies a row or column determines its final value. For each query, the algorithm checks if the corresponding row or column has already been processed (i.e., set by a later query). If not, it adds the contribution of that query to the total sum. The contribution is calculated by multiplying the query value by the number of cells in the row/column that have not been set by any later query.
Step-by-Step Algorithm
- Step 1: Initialize `total_sum` to 0. This variable will store the final sum of the matrix.
- Step 2: Create two boolean arrays, `seen_rows` and `seen_cols`, of size 'n'. Initialize all elements to `False`. These arrays will track whether a row or column has been set by a later query.
- Step 3: Initialize `num_rows_set` and `num_cols_set` to 0. These variables will keep track of the number of rows and columns that have been set.
- Step 4: Iterate through the `queries` array in reverse order.
- Step 5: For each query, determine the `query_type`, `index`, and `val`.
- Step 6: If `query_type` is 0 (row query), check if `seen_rows[index]` is `False`. If it is, it means this is the last time this row is being set. Update `total_sum` by adding `val * (n - num_cols_set)`. Set `seen_rows[index]` to `True` and increment `num_rows_set`.
- Step 7: If `query_type` is 1 (column query), check if `seen_cols[index]` is `False`. If it is, it means this is the last time this column is being set. Update `total_sum` by adding `val * (n - num_rows_set)`. Set `seen_cols[index]` to `True` and increment `num_cols_set`.
- Step 8: After iterating through all the queries, return `total_sum`.
Key Insights
- Insight 1: Processing queries in reverse order simplifies the logic. The last query affecting a row or column determines its final value.
- Insight 2: Using boolean arrays to track which rows and columns have been set avoids unnecessary recalculations and ensures that we only consider the effect of the 'last' query.
- Insight 3: Optimization is achieved by only updating the sum based on the unset rows/columns at the time of processing a particular query.
Complexity Analysis
Time Complexity: O(q)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table.
Companies
Asked at: Sprinklr.