Optimize Approach using prefix sum + Brute Force
\n# Approach\n\u2022 Brute Force was to increase the whole submatrix together\n\u2022 Optimized Approach was navigated by Hints\n# Code\njava []\nclass Solution
\n# Approach\n\u2022 Brute Force was to increase the whole submatrix together\n\u2022 Optimized Approach was navigated by Hints\n# Code\n```java []\nclass Solution {\n    public int[][] rangeAddQueries(int n, int[][] queries) {\n        int[][] res = new int[n][n];\n        for(int[] val:queries){\n            int row1 = val[0];\n            int col1= val[1];\n            int row2=val[2];\n            int col2 = val[3];\n            for(int i=row1;i<=row2;i++){\n                res[i][col1]++;\n                if(col2+1 <n){\n                    res[i][col2+1]--;\n                }\n            }\n        }\n        for(int i=0;i<n;i++){\n            for(int j=0;j<n;j++){\n                if(j-1>=0){\n                    res[i][j]+=res[i][j-1];\n                }\n            }\n        }\n        return res;\n\n    }\n}\n```\n**Brute Force**\n\n```java []\nclass Solution {\n    public int[][] rangeAddQueries(int n, int[][] queries) {\n        int[][] res = new int[n][n];\n        for (int[] val : queries) {\n            int topRow = val[0];\n            int topCol = val[1];\n            int botRow = val[2];\n            int botCol = val[3];\n            for (int i = topRow; i <= botRow; i++) {\n                for (int j = topCol; j <= botCol; j++) {\n                    res[i][j]++;\n                }\n            }\n        }\n        return res;\n\n    }\n}\n```
['Java']