| 30 | return pq.peek(); |
| 31 | } |
| 32 | public int[][] largestLocal(int[][] grid) { |
| 33 | //priorityQueue to return the maximum values out of 9 values (3*3 matrix) |
| 34 | PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); |
| 35 | int rows=grid.length; |
| 36 | int cols=grid[0].length; |
| 37 | int result[][] = new int[rows-2][cols-2]; |
| 38 | //ietrate the starting cells of all possible 3*3 matirx |
| 39 | //TC-> (n-2 * n-2) |
| 40 | for(int i=0;i<rows-2;i++){ |
| 41 | for(int j=0;j<cols-2;j++){ |
| 42 | //update the queue and return the max element |
| 43 | result[i][j] = findMax(grid,i,j,pq); |
| 44 | } |
| 45 | } |
| 46 | return result; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | //Note-> this is slower than the brute force approach but it can be helpful in finding the sum of all 3*3 matrix, i.e instead of a pq, use a prefix sum. |