-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1329-sort-the-matrix-diagonally.cpp
90 lines (84 loc) · 2.04 KB
/
1329-sort-the-matrix-diagonally.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
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
#include<bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
class Solution {
public:
Solution()
{
std::ios::sync_with_stdio(false);
}
vector<vector<int>> diagonalSort(vector<vector<int>>& mat)
{
int m=mat.size();
int n=mat[0].size();
vector<vector<int>>diag;
int num_of_diag=m+n-1;
for(int i=0; i<num_of_diag; i++)
{
if(i<m)
{
int x=i,y=0;
vector<int>v1;
while(x<m&&y<n)
{
v1.push_back(mat[x][y]);
x++;
y++;
}
diag.push_back(v1);
sort(all(diag[i]));
// for(auto x: diag[i])
// cout<<x<<" ";
// cout<<endl;
}
else
{
int x=0,y=i-m+1;
vector<int>v1;
while(x<m&&y<n)
{
v1.push_back(mat[x][y]);
x++;
y++;
}
diag.push_back(v1);
sort(all(diag[i]));
// for(auto x: diag[i])
// cout<<x<<" ";
// cout<<endl;
}
}
for(int i=0; i<num_of_diag; i++)
{
if(i<m)
{
int x=i,y=0,j=0;
while(x<m&&y<n)
{
mat[x][y]=diag[i][j];
x++;
y++;
j++;
}
}
else
{
int x=0,y=i-m+1,j=0;
while(x<m&&y<n)
{
mat[x][y]=diag[i][j];
x++;
y++;
j++;
}
}
}
// f(i,0,m)
// {
// f(j,0,n)
// cout<<mat[i][j]<<" ";
// cout<<endl;
// }
return mat;
}
};