Skip to content

Commit 8fff439

Browse files
committed
update 1030.matrix-cells-in-distance-order.java
1 parent 962f928 commit 8fff439

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* @lc app=leetcode id=1030 lang=java
3+
*
4+
* [1030] Matrix Cells in Distance Order
5+
*
6+
* https://leetcode.com/problems/matrix-cells-in-distance-order/description/
7+
*
8+
* algorithms
9+
* Easy (65.08%)
10+
* Likes: 173
11+
* Dislikes: 83
12+
* Total Accepted: 20.1K
13+
* Total Submissions: 30.9K
14+
* Testcase Example: '1\n2\n0\n0'
15+
*
16+
* We are given a matrix with R rows and C columns has cells with integer
17+
* coordinates (r, c), where 0 <= r < R and 0 <= c < C.
18+
*
19+
* Additionally, we are given a cell in that matrix with coordinates (r0, c0).
20+
*
21+
* Return the coordinates of all cells in the matrix, sorted by their distance
22+
* from (r0, c0) from smallest distance to largest distance.  Here, the
23+
* distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance,
24+
* |r1 - r2| + |c1 - c2|.  (You may return the answer in any order that
25+
* satisfies this condition.)
26+
*
27+
*
28+
*
29+
*
30+
* Example 1:
31+
*
32+
*
33+
* Input: R = 1, C = 2, r0 = 0, c0 = 0
34+
* Output: [[0,0],[0,1]]
35+
* Explanation: The distances from (r0, c0) to other cells are: [0,1]
36+
*
37+
*
38+
*
39+
* Example 2:
40+
*
41+
*
42+
* Input: R = 2, C = 2, r0 = 0, c0 = 1
43+
* Output: [[0,1],[0,0],[1,1],[1,0]]
44+
* Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
45+
* The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.
46+
*
47+
*
48+
*
49+
* Example 3:
50+
*
51+
*
52+
* Input: R = 2, C = 3, r0 = 1, c0 = 2
53+
* Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
54+
* Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
55+
* There are other answers that would also be accepted as correct, such as
56+
* [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].
57+
*
58+
*
59+
*
60+
*
61+
* Note:
62+
*
63+
*
64+
* 1 <= R <= 100
65+
* 1 <= C <= 100
66+
* 0 <= r0 < R
67+
* 0 <= c0 < C
68+
*
69+
*
70+
*
71+
*
72+
*
73+
*/
74+
75+
// @lc code=start
76+
class Solution {
77+
public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
78+
int[][] ans = new int[R*C][2];
79+
80+
int k = 1;
81+
ans[0][0] = r0;
82+
ans[0][1] = c0;
83+
84+
for(int d=1; d<R+C; d++)
85+
for(int i=0; i<R; i++)
86+
for(int j=0; j<C; j++)
87+
if(Math.abs(i-r0) + Math.abs(j-c0) == d){
88+
ans[k][0] = i;
89+
ans[k][1] = j;
90+
k++;
91+
}
92+
93+
return ans;
94+
}
95+
}
96+
// int n = R*C, k = 0, d = 1;
97+
// ans[k++] = {r0, c0};
98+
// while(d < R+C-1){
99+
// //up
100+
// ans[k++] = {r0-d, c0};
101+
// for(int y = c0+1, x = r0-d+1; y<c0+d && x<r0; y++, x++)
102+
// ans[k++] = {x, y};
103+
// //right
104+
// for(int y = c0+d-1, x = r0+1; y>c0 && x<r0+d; y--, x++)
105+
// ans[k++] = {x, y};
106+
// //down
107+
// for(int y = c0+1, x = r0-d+1; y<c0+d && x<r0; y--, x--)
108+
// ans[k++] = {x, y};
109+
// for(int y = c0+1, x = r0-d+1; y<c0+d && x<r0; y++, x++)
110+
// ans[k++] = {x, y};
111+
// }
112+
// @lc code=end

0 commit comments

Comments
 (0)