-
Notifications
You must be signed in to change notification settings - Fork 3
/
1329.cpp
31 lines (29 loc) · 982 Bytes
/
1329.cpp
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
class Solution {
public:
vector<vector<int>> diagonalSort(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
int base = m - 1;
int collectionSize = m + n - 1;
vector<vector<int>> collection(collectionSize);
vector<int> collectionIdx(collectionSize, 0);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int index = j - i + base;
collection[index].push_back(mat[i][j]);
}
}
for (int i = 0; i < collectionSize; ++i) {
sort(collection[i].begin(), collection[i].end());
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int index = j - i + base;
int num = collection[index][collectionIdx[index]];
mat[i][j] = num;
collectionIdx[index]++;
}
}
return mat;
}
};