JAVA | 2 solutions | Brute force and optimized ✅
Please Upvote :D\n---\n##### 1. Brute force approach:\njava []\nclass Solution {\n    public int[][] rangeAddQueries(int n, int[][] queries) {\n        int[][] 
# Please Upvote :D\n---\n##### 1. Brute force approach:\n``` java []\nclass Solution {\n    public int[][] rangeAddQueries(int n, int[][] queries) {\n        int[][] a = new int[n][n];\n        for (int[] q : queries) {\n            int r1 = q[0], r2 = q[2];   // row1, row2\n            int c1 = q[1], c2 = q[3];   // col1, col2\n            // iterate over the matrix \n            // with corners row1, col1 and row2, col2 \n            for (int i = r1; i <= r2; i++) {\n                for (int j = c1; j <= c2; j++) {\n                    a[i][j]++;  // increment\n                }\n            }\n        }\n        \n        return a;   // return the matrix\n    }\n}\n\n// TC: O(m * n^2), SC: O(n^2)\n```\n---\n##### 2. Optimized approach:\n``` java []\nclass Solution {\n    public int[][] rangeAddQueries(int n, int[][] queries) {\n        int[][] a = new int[n][n];\n        for (int[] q : queries) {\n            int r1 = q[0], r2 = q[2];\n            int c1 = q[1], c2 = q[3];\n            for (int i = r1; i <= r2; i++) {\n                a[i][c1]++;\n            }\n            if (c2 + 1 < n) {\n                for (int i = r1; i <= r2; i++) {\n                    a[i][c2 + 1]--;\n                }\n            }\n        }\n        \n        for (int i = 0; i < n; i++) {\n            for (int j = 1; j < n; j++) {\n                a[i][j] += a[i][j - 1];\n            }\n        }\n        \n        return a;\n    }\n}\n\n// TC: O(m * n) + O(n ^ 2), SC: O(n ^ 2)\n```\n
['Matrix', 'Java']