forked from skylarbpayne/interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonal_looping.cpp
More file actions
47 lines (40 loc) · 1.18 KB
/
diagonal_looping.cpp
File metadata and controls
47 lines (40 loc) · 1.18 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
/**
* Author: Skylar Payne
* Date: January 7, 2015
* Make a sorted list of integers from a square matrix that is diagonally sorted
**/
#include <iostream>
#include <vector>
#include <assert.h>
void get_sorted_array_from_diagonal_matrix(std::vector<std::vector<int> > const& m, std::vector<int> res) {
if(m.empty()) {
return;
}
res.resize(m.size() * m[0].size());
std::pair<int,int> diag_start(0, 0);
int ind = 0;
while(ind < res.size()) {
int i = diag_start.first;
int j = diag_start.second;
while(0 <= i && i < m.size() && 0 <= j && j < m[0].size()) {
res[ind++] = m[i++][j--];
}
diag_start.second == m[0].size() - 1 ? ++diag_start.first : ++diag_start.second;
}
}
int main() {
std::vector<std::vector<int> > m1 = {{1,2,4},{3,5,7},{6,8,9}};
std::vector<int> res1;
get_sorted_array_from_diagonal_matrix(m1, res1);
std::vector<std::vector<int> > m2 = {{1,2,4,7},{3,5,8,10},{6,9,11,12}};
std::vector<int> res2;
get_sorted_array_from_diagonal_matrix(m2, res2);
for(int i = 1; i < res1.size(); ++i) {
assert(res1[i-1] <= res1[i]);
}
for(int i = 1; i < res2.size(); ++i) {
assert(res2[i-1] <= res2[i]);
}
std::cout << "All tests passed" << std::endl;
return 0;
}