Skip to content

Commit 03b2fc6

Browse files
committed
Runtime: 13 ms (Top 88.98%) | Memory: 44.4 MB (Top 93.55%)
1 parent e69295e commit 03b2fc6

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,73 @@
1+
// Runtime: 13 ms (Top 88.98%) | Memory: 44.4 MB (Top 93.55%)
12
//--------------------Method 1----------------------
23

34
class Solution {
45
public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
5-
6+
67
int [][]res=new int[rows*cols][2];
7-
8+
89
int idx=0;
9-
10+
1011
for(int i=0;i<rows;i++){
1112
for(int j=0;j<cols;j++){
1213
res[idx][0]=i;
1314
res[idx][1]=j;
1415
idx++;
1516
}
1617
}
17-
18+
1819
Arrays.sort(res,(a,b)->{
1920
int d1=Math.abs(a[0]-rCenter)+Math.abs(a[1]-cCenter);
2021
int d2=Math.abs(b[0]-rCenter)+Math.abs(b[1]-cCenter);
21-
22+
2223
return d1-d2;
2324
});
24-
25+
2526
return res;
2627
}
2728
}
2829

2930
//--------------------Method 2--------------------
3031

3132
// 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+
// }
6263
// }
6364

6465
// 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;
7272
// }
73+
// }

0 commit comments

Comments
 (0)