1
+ // Runtime: 13 ms (Top 88.98%) | Memory: 44.4 MB (Top 93.55%)
1
2
//--------------------Method 1----------------------
2
3
3
4
class Solution {
4
5
public int [][] allCellsDistOrder (int rows , int cols , int rCenter , int cCenter ) {
5
-
6
+
6
7
int [][]res =new int [rows *cols ][2 ];
7
-
8
+
8
9
int idx =0 ;
9
-
10
+
10
11
for (int i =0 ;i <rows ;i ++){
11
12
for (int j =0 ;j <cols ;j ++){
12
13
res [idx ][0 ]=i ;
13
14
res [idx ][1 ]=j ;
14
15
idx ++;
15
16
}
16
17
}
17
-
18
+
18
19
Arrays .sort (res ,(a ,b )->{
19
20
int d1 =Math .abs (a [0 ]-rCenter )+Math .abs (a [1 ]-cCenter );
20
21
int d2 =Math .abs (b [0 ]-rCenter )+Math .abs (b [1 ]-cCenter );
21
-
22
+
22
23
return d1 -d2 ;
23
24
});
24
-
25
+
25
26
return res ;
26
27
}
27
28
}
28
29
29
30
//--------------------Method 2--------------------
30
31
31
32
// class Solution {
32
- // public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
33
-
34
- // boolean [][]vis=new boolean[rows][cols];
35
- // int [][]ans=new int[rows*cols][2];
36
-
37
- // Queue<Pair> q=new LinkedList<>();
38
- // q.add(new Pair(rCenter,cCenter));
39
- // int idx=0;
40
- // vis[rCenter][cCenter]=true;
41
- // int [][]dir={{0,1},{1,0},{-1,0},{0,-1}};
42
-
43
- // while(!q.isEmpty()){
44
- // Pair curr=q.remove();
45
- // ans[idx][0]=curr.r;
46
- // ans[idx][1]=curr.c;
47
- // idx++;
48
-
49
- // for(int []d:dir){
50
- // int nr=curr.r+d[0];
51
- // int nc=curr.c+d[1];
52
-
53
- // if(nr>=0 && nr<rows && nc>=0 && nc<cols && !vis[nr][nc]){
54
- // vis[nr][nc]=true;
55
- // q.add(new Pair(nr,nc));
56
- // }
57
- // }
58
- // }
59
-
60
- // return ans;
61
- // }
33
+ // public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
34
+
35
+ // boolean [][]vis=new boolean[rows][cols];
36
+ // int [][]ans=new int[rows*cols][2];
37
+
38
+ // Queue<Pair> q=new LinkedList<>();
39
+ // q.add(new Pair(rCenter,cCenter));
40
+ // int idx=0;
41
+ // vis[rCenter][cCenter]=true;
42
+ // int [][]dir={{0,1},{1,0},{-1,0},{0,-1}};
43
+
44
+ // while(!q.isEmpty()){
45
+ // Pair curr=q.remove();
46
+ // ans[idx][0]=curr.r;
47
+ // ans[idx][1]=curr.c;
48
+ // idx++;
49
+
50
+ // for(int []d:dir){
51
+ // int nr=curr.r+d[0];
52
+ // int nc=curr.c+d[1];
53
+
54
+ // if(nr>=0 && nr<rows && nc>=0 && nc<cols && !vis[nr][nc]){
55
+ // vis[nr][nc]=true;
56
+ // q.add(new Pair(nr,nc));
57
+ // }
58
+ // }
59
+ // }
60
+
61
+ // return ans;
62
+ // }
62
63
// }
63
64
64
65
// class Pair{
65
- // int r;
66
- // int c;
67
-
68
- // public Pair(int r, int c){
69
- // this.r=r;
70
- // this.c=c;
71
- // }
66
+ // int r;
67
+ // int c;
68
+
69
+ // public Pair(int r, int c){
70
+ // this.r=r;
71
+ // this.c=c;
72
72
// }
73
+ // }
0 commit comments