Skip to content

Commit 7b125c6

Browse files
committed
Add solution #1329
1 parent a435413 commit 7b125c6

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,233 LeetCode solutions in JavaScript
1+
# 1,234 LeetCode solutions in JavaScript
22

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

@@ -1007,6 +1007,7 @@
10071007
1325|[Delete Leaves With a Given Value](./solutions/1325-delete-leaves-with-a-given-value.js)|Medium|
10081008
1326|[Minimum Number of Taps to Open to Water a Garden](./solutions/1326-minimum-number-of-taps-to-open-to-water-a-garden.js)|Hard|
10091009
1328|[Break a Palindrome](./solutions/1328-break-a-palindrome.js)|Medium|
1010+
1329|[Sort the Matrix Diagonally](./solutions/1329-sort-the-matrix-diagonally.js)|Medium|
10101011
1331|[Rank Transform of an Array](./solutions/1331-rank-transform-of-an-array.js)|Easy|
10111012
1332|[Remove Palindromic Subsequences](./solutions/1332-remove-palindromic-subsequences.js)|Easy|
10121013
1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](./solutions/1333-filter-restaurants-by-vegan-friendly-price-and-distance.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* 1329. Sort the Matrix Diagonally
3+
* https://leetcode.com/problems/sort-the-matrix-diagonally/
4+
* Difficulty: Medium
5+
*
6+
* A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost
7+
* row or leftmost column and going in the bottom-right direction until reaching the matrix's
8+
* end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix,
9+
* includes cells mat[2][0], mat[3][1], and mat[4][2].
10+
*
11+
* Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return
12+
* the resulting matrix.
13+
*/
14+
15+
/**
16+
* @param {number[][]} mat
17+
* @return {number[][]}
18+
*/
19+
var diagonalSort = function(mat) {
20+
const rows = mat.length;
21+
const cols = mat[0].length;
22+
const diagonals = new Map();
23+
24+
for (let i = 0; i < rows; i++) {
25+
for (let j = 0; j < cols; j++) {
26+
const diagonalKey = i - j;
27+
if (!diagonals.has(diagonalKey)) {
28+
diagonals.set(diagonalKey, []);
29+
}
30+
diagonals.get(diagonalKey).push(mat[i][j]);
31+
}
32+
}
33+
34+
diagonals.forEach(values => values.sort((a, b) => a - b));
35+
36+
for (let i = 0; i < rows; i++) {
37+
for (let j = 0; j < cols; j++) {
38+
const diagonalKey = i - j;
39+
mat[i][j] = diagonals.get(diagonalKey).shift();
40+
}
41+
}
42+
43+
return mat;
44+
};

0 commit comments

Comments
 (0)