File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 273927393442|[ Maximum Difference Between Even and Odd Frequency I] ( ./solutions/3442-maximum-difference-between-even-and-odd-frequency-i.js ) |Easy|
274027403443|[ Maximum Manhattan Distance After K Changes] ( ./solutions/3443-maximum-manhattan-distance-after-k-changes.js ) |Medium|
274127413445|[ 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|
274227433450|[ Maximum Students on a Single Bench] ( ./solutions/3450-maximum-students-on-a-single-bench.js ) |Easy|
274327443452|[ Sum of Good Numbers] ( ./solutions/3452-sum-of-good-numbers.js ) |Easy|
274427453460|[ Longest Common Prefix After at Most One Removal] ( ./solutions/3460-longest-common-prefix-after-at-most-one-removal.js ) |Medium|
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments