leetcode1030. Matrix Cells in Distance Order
We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.
Additionally, we are given a cell in that matrix with coordinates (r0, c0).
Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance. Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|. (You may return the answer in any order that satisfies this condition.)
Example :
Input: R = 1, C = 2, r0 = 0, c0 = 0
Output: [[0,0],[0,1]]
Input: R = 2, C = 2, r0 = 0, c0 = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Input: R = 2, C = 3, r0 = 1, c0 = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/matrix-cells-in-distance-order
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Thought and method:
1、directly sort
Obviously, the easiest solution is to sort by distance, and then output the coordinates in turn.
However,it is a inefficient way.
Time complexity : O(R * C * log(R * C))
Space complexity: O(R * C)
2、like buck sort
Traversing all coordinates, grouping according to the size of the distance, the equal distance of each group, according to the principle of distance from small to large, traversing all the buckets, and output the result.
It is the fastest way in theory.
Time complexity : O(R * C )
Space complexity: O(R * C)
3、geometric method
We can think of the matrix as a graph in rectangular coordinates, so the coordinates in question can be transformed into a square that chooses 45 degrees.
Starting from (r0, c0), expand outward according to the distance from Manhattan, traversing different sizes of squares from the inside to the outside.In the code, I use clockwise traversal.From left to up to right to down. However, you can begin at the any point and with and direction.
Time complexity : O((R+C)^2 )
Space complexity: O(R * C)
So it’s not a very quick way to do it, but it opens up my mind to the idea that problems like this can be thought of geometrically.
Code:
1、
func allCellsDistOrder(R int, C int, r0 int, c0 int) [][]int {
result := make([][]int,R*C)
counter := 0
//create a intervals
for i := 0;i < R;i++ {
for j := 0;j < C;j++ {
result[counter] = []int{i,j}
counter++
}
}
//directly sort and output according to the order
sort.Slice(result,func(i,j int) bool{
dis := int(math.Abs(float64(result[i][0] - r0))) + int(math.Abs(float64(result[i][1] - c0)))
dis2 := int(math.Abs(float64(result[j][0] - r0))) + int(math.Abs(float64(result[j][1] - c0)))
return dis < dis2
})
return result
}
2、
func allCellsDistOrder(R int, C int, r0 int, c0 int) [][]int {
m := make(map[int][][]int)
//grouping according to the size of the distance
distmax := 0
for r := 0; r < R; r++ {
for c := 0; c < C; c++ {
dist := int(math.Abs(float64(r - r0))) + int(math.Abs(float64(c - c0)))
if dist > distmax {
distmax = dist
}
m[dist] = append(m[dist], []int{r, c})
}
}
result := make([][]int, R * C)
//according to the principle of distance from small to large, traversing all
j := 0
for i := 0; i <= distmax; i++ {
for _, v := range m[i] {
result[j] = v
j++
}
}
return result
}
3、
func allCellsDistOrder(R int, C int, r0 int, c0 int) [][]int {
result := make([][]int, R*C)
result[0] = []int{r0, c0}
counter := 1
r, c := r0, c0
for counter < R * C {
r-- // start:r0-1,c0
//clock wise
// left
for c < c0 {
if r >= 0 && c >= 0 {
result[counter] = []int{r, c}
counter++
}
r--
c++
}
// up
for r < r0 {
if r >= 0 && c < C {
result[counter] = []int{r, c}
counter++
}
r++
c++
}
//right
for c > c0 {
if r < R && c < C {
result[counter] = []int{r, c}
counter++
}
r++
c--
}
//down
for r > r0 {
if r < R && c >= 0 {
result[counter] = []int{r, c}
counter++
}
r--
c--
}
}
return result
}
Test
from up to down is method : 1 3 2
In the actual test, the geometric method exceeds the time limit, and the “bucket sequencing” verifies its high efficiency
1、
Input: R = 1, C = 2, r0 = 0, c0 = 0
Output:
2、
Input: R = 2, C = 2, r0 = 0, c0 = 1
Output:
3、
Input: R = 2, C = 3, r0 = 1, c0 = 2
Output: