-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1329.cpp
More file actions
40 lines (36 loc) · 1.02 KB
/
1329.cpp
File metadata and controls
40 lines (36 loc) · 1.02 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
class Solution {
public:
vector<vector<int>> diagonalSort(vector<vector<int>>& mat) { //O(NM)
int m = mat.size();
int n = mat[0].size();
for(int i = m-1; i >= 0; i--){
int x = i; int y = 0;
vector<int> v;
while(x < m && y < n){
v.push_back(mat[x][y]);
x++; y++;
}
sort(v.begin(), v.end());
x = i; y = 0;
for(int j = 0; j < v.size(); j++){
mat[x][y] = v[j];
x++; y++;
}
}
for(int j = 1; j < n; j++){
int x = 0; int y = j;
vector<int> v;
while(x < m && y < n){
v.push_back(mat[x][y]);
x++; y++;
}
sort(v.begin(), v.end());
x = 0; y = j;
for(int i = 0; i < v.size(); i++){
mat[x][y] = v[i];
x++; y++;
}
}
return mat;
}
};