-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3446.cpp
More file actions
27 lines (27 loc) · 788 Bytes
/
3446.cpp
File metadata and controls
27 lines (27 loc) · 788 Bytes
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
class Solution {
public:
vector<vector<int>> sortMatrix(vector<vector<int>>& grid) {
int n = grid.size();
unordered_map<int, vector<int>> mp;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int key = j - i;
mp[key].push_back(grid[i][j]);
}
}
int m = mp.size();
unordered_map<int, int> k2i;
for (auto& [k, v] : mp) {
sort(v.begin(), v.end());
if (k <= 0) reverse(v.begin(), v.end());
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int key = j - i;
grid[i][j] = mp[key][k2i[key]];
k2i[key]++;
}
}
return grid;
}
};