leetcode1030. Matrix Cells in Distance Order

该博客探讨了LeetCode1030题目的解决方案,比较了直接排序、桶排序和几何方法在解决按曼哈顿距离排序矩阵单元格问题的效率和复杂度。几何方法虽然不是最快但启发了用几何思维解决问题的可能性。在实际测试中,几何方法超出了时间限制,验证了桶排序的高效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值