-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy path3446. Sort Matrix by Diagonals.cpp
More file actions
70 lines (70 loc) · 1.68 KB
/
3446. Sort Matrix by Diagonals.cpp
File metadata and controls
70 lines (70 loc) · 1.68 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
class Solution {
public:
vector<vector<int>> sortMatrix(vector<vector<int>>& grid) {
vector<vector<int>> ans;
vector<int> t;
int cnt = 1;
int j=0;
for(int i=grid.size()-1;i>=0;i--){
int p=i;
while(j < grid.size() && p < grid.size()){
t.push_back(grid[p][j]);
p++;
j++;
}
sort(t.begin(), t.end());
reverse(t.begin(), t.end());
ans.push_back(t);
t.clear();
j=0;
}
vector<vector<int>> a1;
int r = 1;
for(int k=1;k<grid.size();k++){
int q = 0;
int d = r;
while(q < grid.size() && d < grid.size()){
t.push_back(grid[q][d]);
q++;
d++;
}
sort(t.begin(), t.end());
a1.push_back(t);
t.clear();
r++;
}
for(int i=0;i<a1.size();i++){
ans.push_back(a1[i]);
}
j = grid.size()-1;
int p = 0,q=0;
for(int i=grid.size()-1;i>=0;i--){
int r = i;
while(q < ans[p].size()){
grid[r][j] = ans[p][q];
q++;
r++;
j++;
}
j=0;
q=0;
p++;
}
q=0;
j=1;
r=0;
for(int i=1;i<grid.size();i++){
while(q < ans[p].size()){
grid[r][j] = ans[p][q];
q++;
r++;
j++;
}
j=i+1;
r=0;
q=0;
p++;
}
return grid;
}
};