bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2718. Sum of Matrix After Queries

2718. Sum of Matrix After Queries

Leetcode Solutions

Reverse Iteration and Set Tracking

  1. Initialize two sets, rows and cols, to keep track of the rows and columns that have been updated.
  2. Initialize a variable res to store the result, the sum of the matrix after all queries.
  3. Iterate through the queries in reverse order.
    • If the query is of type 0 (update a row) and the row has not been updated before, add the row index to rows and update res by adding vali * n and subtracting vali * len(cols).
    • If the query is of type 1 (update a column) and the column has not been updated before, add the column index to cols and update res by adding vali * n and subtracting vali * len(rows).
  4. Return the value of res.
UML Thumbnail

Brute Force Approach with Direct Matrix Manipulation

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...