Skip to content

Commit f94b387

Browse files
committed
Add solution #1030
1 parent e3aefb6 commit f94b387

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,101 LeetCode solutions in JavaScript
1+
# 1,102 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -837,6 +837,7 @@
837837
1027|[Longest Arithmetic Subsequence](./solutions/1027-longest-arithmetic-subsequence.js)|Medium|
838838
1028|[Recover a Tree From Preorder Traversal](./solutions/1028-recover-a-tree-from-preorder-traversal.js)|Hard|
839839
1029|[Two City Scheduling](./solutions/1029-two-city-scheduling.js)|Medium|
840+
1030|[Matrix Cells in Distance Order](./solutions/1030-matrix-cells-in-distance-order.js)|Easy|
840841
1037|[Valid Boomerang](./solutions/1037-valid-boomerang.js)|Easy|
841842
1038|[Binary Search Tree to Greater Sum Tree](./solutions/1038-binary-search-tree-to-greater-sum-tree.js)|Medium|
842843
1041|[Robot Bounded In Circle](./solutions/1041-robot-bounded-in-circle.js)|Medium|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1030. Matrix Cells in Distance Order
3+
* https://leetcode.com/problems/matrix-cells-in-distance-order/
4+
* Difficulty: Easy
5+
*
6+
* You are given four integers row, cols, rCenter, and cCenter. There is a rows x cols matrix and
7+
* you are on the cell with the coordinates (rCenter, cCenter).
8+
*
9+
* Return the coordinates of all cells in the matrix, sorted by their distance from (rCenter,
10+
* cCenter) from the smallest distance to the largest distance. You may return the answer in any
11+
* order that satisfies this condition.
12+
*
13+
* The distance between two cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|.
14+
*/
15+
16+
/**
17+
* @param {number} rows
18+
* @param {number} cols
19+
* @param {number} rCenter
20+
* @param {number} cCenter
21+
* @return {number[][]}
22+
*/
23+
var allCellsDistOrder = function(rows, cols, rCenter, cCenter) {
24+
const result = [];
25+
for (let row = 0; row < rows; row++) {
26+
for (let col = 0; col < cols; col++) {
27+
result.push([row, col]);
28+
}
29+
}
30+
return result.sort((a, b) => {
31+
return Math.abs(a[0] - rCenter) + Math.abs(a[1] - cCenter)
32+
- Math.abs(b[0] - rCenter) - Math.abs(b[1] - cCenter);
33+
});
34+
};

0 commit comments

Comments
 (0)