✔️ Counting and Reverse Iteration | Solution & Explanation
Approach\ncntR and cntC are the no. of rows and columns resp. which are not set yet.\nwe are iterating the queries in reverse, since the last elements in the qu
# Approach\n`cntR` and `cntC` are the no. of rows and columns resp. which are not set yet.\nwe are iterating the queries in `reverse`, since the last elements in the query are prefferable since they are the one whose set value is reflected in the matrix , they `overwrite the others`.\nTherefore we are tracking record of the row and column which are visited or not, if it is visited, we should not evaluate its set value and if it is not visited we evaluate and reduce the `cnt by 1` for the corresponding row or column and mark it visited that it should not be evaluted afterwards. \n\n# Complexity\n- Time complexity: $$O(len(queries))$$\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution:\n    def matrixSumQueries(self, n: int, queries: List[List[int]]) -> int:\n        ans = 0\n        cntR, cntC = n, n\n        R = [True] * n\n        C = [True] * n\n        for t, i, v in queries[::-1]:\n            if t == 0 and R[i]:\n                ans += v * cntC\n                cntR -= 1\n                R[i] = False\n            elif t == 1 and C[i]:\n                ans += v * cntR\n                cntC -= 1\n                C[i] = False\n        return ans\n\n```
['Matrix', 'Counting', 'Python', 'Python3']