Skip to content

Commit c766849

Browse files
committed
Add solution #3446
1 parent daf3e8b commit c766849

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,7 @@
27392739
3442|[Maximum Difference Between Even and Odd Frequency I](./solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js)|Easy|
27402740
3443|[Maximum Manhattan Distance After K Changes](./solutions/3443-maximum-manhattan-distance-after-k-changes.js)|Medium|
27412741
3445|[Maximum Difference Between Even and Odd Frequency II](./solutions/3445-maximum-difference-between-even-and-odd-frequency-ii.js)|Hard|
2742+
3446|[Sort Matrix by Diagonals](./solutions/3446-sort-matrix-by-diagonals.js)|Medium|
27422743
3450|[Maximum Students on a Single Bench](./solutions/3450-maximum-students-on-a-single-bench.js)|Easy|
27432744
3452|[Sum of Good Numbers](./solutions/3452-sum-of-good-numbers.js)|Easy|
27442745
3460|[Longest Common Prefix After at Most One Removal](./solutions/3460-longest-common-prefix-after-at-most-one-removal.js)|Medium|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 3446. Sort Matrix by Diagonals
3+
* https://leetcode.com/problems/sort-matrix-by-diagonals/
4+
* Difficulty: Medium
5+
*
6+
* You are given an n x n square matrix of integers grid. Return the matrix such that:
7+
* - The diagonals in the bottom-left triangle (including the middle diagonal) are sorted
8+
* in non-increasing order.
9+
* - The diagonals in the top-right triangle are sorted in non-decreasing order.
10+
*/
11+
12+
/**
13+
* @param {number[][]} grid
14+
* @return {number[][]}
15+
*/
16+
var sortMatrix = function(grid) {
17+
const n = grid.length;
18+
const map = new Map();
19+
20+
for (let i = 0; i < n; i++) {
21+
for (let j = 0; j < n; j++) {
22+
const key = i - j;
23+
if (!map.has(key)) {
24+
map.set(key, key < 0
25+
? new PriorityQueue((a, b) => a - b)
26+
: new PriorityQueue((a, b) => b - a));
27+
}
28+
map.get(key).enqueue(grid[i][j]);
29+
}
30+
}
31+
32+
for (let i = 0; i < n; i++) {
33+
for (let j = 0; j < n; j++) {
34+
const key = i - j;
35+
grid[i][j] = map.get(key).dequeue();
36+
}
37+
}
38+
39+
return grid;
40+
};

0 commit comments

Comments
 (0)