MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / findMax

Method findMax

largestLocal.java:12–31  ·  view source on GitHub ↗
(int[][] grid, int startRow, int startCol, PriorityQueue<Integer> pq)

Source from the content-addressed store, hash-verified

10 // Hence for every 3*3 matrix starting at the ith row and jth column ([i,j] cell and j>0) remove the (j-1)th column, add the (j+2)th column and find the maximum
11 // at every new row, reinitialize the queue.
12 public int findMax(int[][] grid, int startRow, int startCol, PriorityQueue<Integer> pq){
13 //for every new row, init the queue and store 9 elements (3*3)
14 // TC -> O(9log9) [for inserting 1 element in pq, tc is logK, where k is number of element, our pq will always have 9 elements at max]
15 if(startCol==0){
16 pq.clear();
17 for(int i=startRow;i<startRow+3;i++){
18 for(int j=startCol;j<startCol+3;j++){
19 pq.offer(grid[i][j]);
20 }
21 }
22 }else{
23 //TC -> 6log9 [remove -> 3log9 + insert 3log9]
24 //for same row, use sliding window to remove the (j-1)th column and add (j+2) column
25 for(int j=startRow;j<startRow+3;j++){
26 pq.remove(grid[j][startCol-1]);
27 pq.offer(grid[j][startCol+2]);
28 }
29 }
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());

Callers 1

largestLocalMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected