-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntiDiagonals.cpp
More file actions
83 lines (63 loc) · 1.24 KB
/
AntiDiagonals.cpp
File metadata and controls
83 lines (63 loc) · 1.24 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
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Give a N*N square matrix, return an array of its anti-diagonals. Look at the example for more details.
Example:
Input:
1 2 3
4 5 6
7 8 9
Return the following :
[
[1],
[2, 4],
[3, 5, 7],
[6, 8],
[9]
]
Input :
1 2
3 4
Return the following :
[
[1],
[2, 3],
[4]
]
LINK: https://www.interviewbit.com/problems/anti-diagonals/
*/
vector<vector<int> > Solution::diagonal(vector<vector<int> > &A) {
int n = A.size();
vector<vector<int> > res;
vector<int> tmp;
int i,j;
for(j=0;j<n;j++){
int jj = j;
for(i=0;i<=j;i++){
tmp.push_back(A[i][jj--]);
}
res.push_back(tmp);
tmp.clear();
}
for(i=1;i<n;i++){
int ii = i;
for(j=n-1;j>=i;j--){
tmp.push_back(A[ii++][j]);
}
res.push_back(tmp);
tmp.clear();
}
return res;
}
/*
Editorial:
vector<vector<int> > Solution::diagonal(vector<vector<int> > &A) {
int n = A.size();
int N = 2*(n-1) + 1;//number of vectors in ans
vector<vector<int>> ans(N);
for(int i = 0;i<n;i++)
{
for(int j = 0;j<n;j++)
ans[i+j].push_back(A[i][j]);//sum of index values in 2d array gives the index in ans
}
return ans;
}
*/