Java|C++| set | math | Greedy Algo
Idea is to check the lastest entry for columns and rows only and \nentries to take in a latest row = (n - number column entries considered till now ) * value of
Idea is to check the lastest entry for columns and rows only and \nentries to take in a latest row = (n - number column entries considered till now ) * value of latest row \nentries to take in a latest column = (n - number row entries considered till now) * value of latest column\n```\nclass Solution {\npublic:\n    long long matrixSumQueries(int n, vector<vector<int>>& q) {\n        long long totalSum = 0, cNo = 0, rNo = 0;\n        unordered_set<int> r, c;\n        for(int indx = q.size()-1; indx > - 1 ; indx--){\n            auto qs = q[indx];\n            if(!qs[0] && r.find(qs[1]) == r.end()){\n                r.insert(qs[1]);\n                rNo++;\n               totalSum += (qs[2] * (n-cNo));\n      \n            }else if(qs[0] && c.find(qs[1]) == c.end()){\n                c.insert(qs[1]);\n                cNo++;\n               totalSum += (qs[2] * (n- rNo));\n            }\n            \n        }\n        return totalSum;\n     \n    }\n};\n```\nJava\n```\nclass Solution {\n    public long matrixSumQueries(int n, int[][] q) {\n        long totalSum = 0, cNo = 0, rNo = 0;\n        HashSet<Integer> r = new HashSet<>(), c = new HashSet<>();\n        for(int indx = q.length-1; indx > - 1 ; indx--){\n            var qs = q[indx];\n            if(qs[0] == 0 && !r.contains(qs[1])){\n                r.add(qs[1]);\n                rNo++;\n               totalSum += (qs[2] * (n-cNo));\n      \n            }else if(qs[0] == 1 && !c.contains(qs[1])){\n                c.add(qs[1]);\n                cNo++;\n               totalSum += (qs[2] * (n- rNo));\n            }\n            \n        }\n        return totalSum;\n    }\n}\n```
[]