1
+ // Runtime: 83 ms (Top 88.06%) | Memory: 75.50 MB (Top 67.16%)
2
+
1
3
class Solution {
2
- static class Quad
3
- {
4
- int x ,y ,price ,dist ;
5
- Quad (int x ,int y ,int price ,int dist )
6
- {
7
- this .x =x ;
8
- this .y =y ;
9
- this .price =price ;
10
- this .dist =dist ;
11
- }
12
- }
13
4
public List <List <Integer >> highestRankedKItems (int [][] grid , int [] pricing , int [] start , int k ) {
14
- List <List <Integer >> ans =new LinkedList <>();
15
- PriorityQueue <Quad > pq =new PriorityQueue <>((a ,b )->{
16
- if (a .dist !=b .dist )
17
- return a .dist -b .dist ;
18
- if (a .price !=b .price )
19
- return a .price -b .price ;
20
- if (a .x !=b .x )
21
- return a .x -b .x ;
22
- return a .y -b .y ;
23
- });
24
- bfs (grid ,start [0 ],start [1 ],pricing [0 ],pricing [1 ],pq );
25
- while (!pq .isEmpty ()&&k -->0 )
26
- {
27
- Quad quad =pq .poll ();
28
- List <Integer > temp =new LinkedList <>();
29
- temp .add (quad .x );
30
- temp .add (quad .y );
31
- ans .add (temp );
5
+ int low = pricing [0 ];
6
+ int high = pricing [1 ];
7
+ int n = grid .length ;
8
+ int m = grid [0 ].length ;
9
+ Queue <Pair > q = new LinkedList <>();
10
+ boolean [][] visited = new boolean [n ][m ];
11
+ ArrayList <Pair > list = new ArrayList <>();
12
+ int dis = 0 ;
13
+ q .add (new Pair (start [0 ], start [1 ], grid [start [0 ]][start [1 ]], 0 ));
14
+ visited [start [0 ]][start [1 ]] = true ;
15
+ // normal bfs starts here.
16
+ while (!q .isEmpty ()) {
17
+ int size = q .size ();
18
+ dis ++;
19
+ while (size -- > 0 ) {
20
+ Pair p = q .remove ();
21
+ int x = p .x ;
22
+ int y = p .y ;
23
+ int val = p .val ;
24
+ // if nearby cells in range add that element to queue and mark the cell as visited.
25
+ if (isInRange (x + 1 , y , n , m , grid , visited , low , high )) {
26
+ q .add (new Pair (x + 1 , y , grid [x + 1 ][y ], dis ));
27
+ visited [x + 1 ][y ] = true ;
28
+ }
29
+ if (isInRange (x - 1 , y , n , m , grid , visited , low , high )) {
30
+ q .add (new Pair (x - 1 , y , grid [x - 1 ][y ], dis ));
31
+ visited [x - 1 ][y ] = true ;
32
+ }
33
+ if (isInRange (x , y + 1 , n , m , grid , visited , low , high )) {
34
+ q .add (new Pair (x , y + 1 , grid [x ][y + 1 ], dis ));
35
+ visited [x ][y + 1 ] = true ;
36
+ }
37
+ if (isInRange (x , y - 1 , n , m , grid , visited , low , high )) {
38
+ q .add (new Pair (x , y - 1 , grid [x ][y - 1 ], dis ));
39
+ visited [x ][y - 1 ] = true ;
40
+ }
41
+ // add every element of queue to list.
42
+ list .add (new Pair (p .x , p .y , p .val , p .dis ));
43
+ }
32
44
}
33
- return ans ;
34
- }
35
- void bfs (int [][] grid ,int i ,int j ,int low ,int high ,PriorityQueue <Quad > pq )
36
- {
37
- Queue <int []> queue =new LinkedList <>();
38
- int m =grid .length ,n =grid [0 ].length ;
39
- if (grid [i ][j ]>=low &&grid [i ][j ]<=high )
40
- pq .add (new Quad (i ,j ,grid [i ][j ],0 ));
41
- grid [i ][j ]=0 ;
42
- queue .add (new int []{i ,j });
43
- int dist =0 ;
44
- int dirs [][]={{1 ,0 },{-1 ,0 },{0 ,1 },{0 ,-1 }};
45
- while (!queue .isEmpty ())
46
- {
47
- ++dist ;
48
- int size =queue .size ();
49
- while (size -->0 )
50
- {
51
- int [] p =queue .poll ();
52
- for (int [] dir :dirs )
53
- {
54
- int newX =dir [0 ]+p [0 ];
55
- int newY =dir [1 ]+p [1 ];
56
- if (newX >=0 &&newY >=0 &&newX <m &&newY <n &&grid [newX ][newY ]!=0 )
57
- {
58
- if (grid [newX ][newY ]>=low &&grid [newX ][newY ]<=high )
59
- pq .add (new Quad (newX ,newY ,grid [newX ][newY ],dist ));
60
- queue .add (new int []{newX ,newY });
61
- grid [newX ][newY ]=0 ;
45
+ ArrayList <Pair > list2 = new ArrayList <>();
46
+ for (Pair p : list ) {
47
+ // remove the values from list if they are not in pricing range and add that to list2.
48
+ if (p .val != 1 && p .val >= low && p .val <= high ) {
49
+ list2 .add (new Pair (p .x , p .y , p .val , p .dis ));
50
+ }
51
+ }
52
+ // Most important part. Sorting the list2 on basis of the conditions given in question. Higher rank cells must be added first(before) in the list.
53
+ Collections .sort (list2 , new Comparator <Pair >() {
54
+ @ Override
55
+ public int compare (Pair p1 , Pair p2 ) {
56
+ // first check on basis of distance (high value, then add it before the second pair).
57
+ if (p1 .dis > p2 .dis ) {
58
+ return 1 ;
59
+ } else if (p1 .dis < p2 .dis ) {
60
+ return -1 ;
61
+ } else {
62
+ // if distances are equal, then second check on basis of value (high value, then add it before the second pair).
63
+ if (p1 .val > p2 .val ) {
64
+ return 1 ;
65
+ } else if (p1 .val < p2 .val ) {
66
+ return -1 ;
67
+ } else {
68
+ // if distances and values are equal, then third check on basis of x-coordinate (high value, then add it before the second pair).
69
+ if (p1 .x > p2 .x ) {
70
+ return 1 ;
71
+ } else if (p1 .x < p2 .x ) {
72
+ return -1 ;
73
+ } else {
74
+ // if distances, values and x-coordinate are equal, then fourth check on basis of y-coordinate (high value, then add it before the second pair).).
75
+ if (p1 .y > p2 .y ) {
76
+ return 1 ;
77
+ } else {
78
+ return -1 ;
79
+ }
80
+ }
62
81
}
63
82
}
64
83
}
65
-
84
+ });
85
+ List <List <Integer >> ans = new ArrayList <>();
86
+ // selecting only first k values from list2, and adding there x,y - coordinates in ans arraylist.
87
+ for (Pair p : list2 ) {
88
+ if (k == 0 ) {
89
+ break ;
90
+ }
91
+ k --;
92
+ List <Integer > temp = new ArrayList <>();
93
+ temp .add (p .x );
94
+ temp .add (p .y );
95
+ ans .add (new ArrayList <>(temp ));
66
96
}
97
+ return ans ;
98
+ }
99
+
100
+ // check whether i, j is in range or not (ignore visited cells and cells with zero value)
101
+ private boolean isInRange (int i , int j , int n , int m , int [][] grid , boolean [][] visited , int low , int high ) {
102
+ if (i < 0 || j < 0 || i >= n || j >= m || grid [i ][j ] == 0 || visited [i ][j ]) {
103
+ return false ;
104
+ }
105
+ return true ;
67
106
}
68
-
107
+ }
69
108
70
- }
109
+ // Pair class for x-coordinate, y-coordinate, grid value, distance from start point
110
+ class Pair {
111
+ int x ; int y ; int val ; int dis ;
112
+
113
+ public Pair (int x , int y , int val , int dis ) {
114
+ this .x = x ;
115
+ this .y = y ;
116
+ this .val = val ;
117
+ this .dis = dis ;
118
+ }
119
+ }
0 commit comments