-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28_Sort_Matrix_by_Diagonals.cpp
More file actions
97 lines (67 loc) · 2.3 KB
/
Copy path28_Sort_Matrix_by_Diagonals.cpp
File metadata and controls
97 lines (67 loc) · 2.3 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// 3446. Sort Matrix by Diagonals
// You are given an n x n square matrix of integers grid. Return the matrix such that:
// The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order.
// The diagonals in the top-right triangle are sorted in non-decreasing order.
// Example 1:
// Input: grid = [[1,7,3],[9,8,2],[4,5,6]]
// Output: [[8,2,3],[9,6,7],[4,5,1]]
// Explanation:
// The diagonals with a black arrow (bottom-left triangle) should be sorted in non-increasing order:
// [1, 8, 6] becomes [8, 6, 1].
// [9, 5] and [4] remain unchanged.
// The diagonals with a blue arrow (top-right triangle) should be sorted in non-decreasing order:
// [7, 2] becomes [2, 7].
// [3] remains unchanged.
// Example 2:
// Input: grid = [[0,1],[1,2]]
// Output: [[2,1],[1,0]]
// Explanation:
// The diagonals with a black arrow must be non-increasing, so [0, 2] is changed to [2, 0]. The other diagonals are already in the correct order.
// Example 3:
// Input: grid = [[1]]
// Output: [[1]]
// Explanation:
// Diagonals with exactly one element are already in order, so no changes are needed.
// Constraints:
// grid.length == grid[i].length == n
// 1 <= n <= 10
// -105 <= grid[i][j] <= 105
class Solution
{
public:
vector<vector<int>> sortMatrix(vector<vector<int>> &grid)
{
int n = grid.size(), m = grid[0].size();
unordered_map<int, priority_queue<int>> maxHeaps;
unordered_map<int, priority_queue<int, vector<int>, greater<int>>> minHeaps;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
int key = i - j;
if (key < 0)
minHeaps[key].push(grid[i][j]);
else
maxHeaps[key].push(grid[i][j]);
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
int key = i - j;
if (key < 0)
{
grid[i][j] = minHeaps[key].top();
minHeaps[key].pop();
}
else
{
grid[i][j] = maxHeaps[key].top();
maxHeaps[key].pop();
}
}
}
return grid;
}
};