-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution1030.cs
34 lines (28 loc) · 886 Bytes
/
Solution1030.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution1030
{
/// <summary>
/// 1030. Matrix Cells in Distance Order - Easy
/// <a href="https://leetcode.com/problems/matrix-cells-in-distance-order</a>
/// </summary>
public int[][] AllCellsDistOrder(int rows, int cols, int rCenter, int cCenter)
{
var cells = new int[rows * cols][];
int i = 0;
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
cells[i++] = [r, c];
}
}
Array.Sort(cells, (a, b) => Distance(a, rCenter, cCenter) - Distance(b, rCenter, cCenter));
return cells;
}
private int Distance(int[] cell, int rCenter, int cCenter)
{
return Math.Abs(cell[0] - rCenter) + Math.Abs(cell[1] - cCenter);
}
}