-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
83 lines (70 loc) · 2.07 KB
/
Solution.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
#include <queue>
#include <vector>
using namespace std;
class Solution {
private:
// Kahn's algorithm: enqueue and process zero in-degree nodes iteratively
vector<int> topologicalSort(int const k,
vector<vector<int>> const& conditions) {
vector<vector<int>> graph(k);
vector<int> inDegree(k, 0);
for (auto const& condition : conditions) {
// Sort from top-to-bottom / left-to-right
// convert 1-indexed to 0-index
int from = condition[0] - 1;
int to = condition[1] - 1;
graph[from].push_back(to);
++inDegree[to];
}
queue<int> zeroDegree;
for (int i = 0; i < k; ++i) {
if (inDegree[i] == 0) {
zeroDegree.push(i);
}
}
vector<int> result;
result.reserve(k);
while (!zeroDegree.empty()) {
int v = zeroDegree.front();
zeroDegree.pop();
result.push_back(v + 1); // convert back to 1-index
for (int neighbour : graph[v]) {
if (--inDegree[neighbour] == 0) {
zeroDegree.push(neighbour);
}
}
}
if (result.size() != k) {
return {}; // Cycle detection
}
return result;
}
public:
// Contains 1 to k
vector<vector<int>> buildMatrix(int k,
vector<vector<int>>& rowConditions,
vector<vector<int>>& colConditions) {
vector<int> rowOrder = topologicalSort(k, rowConditions);
if (rowOrder.empty()) {
return {};
}
vector<int> columnOrder = topologicalSort(k, colConditions);
if (columnOrder.empty()) {
return {};
}
// Stores the index where the k-th integer should be placed
vector<int> rowPosition(k);
vector<int> columnPosition(k);
for (int i = 0; i < k; ++i) {
rowPosition[rowOrder[i] - 1] = i;
columnPosition[columnOrder[i] - 1] = i;
}
vector<vector<int>> matrix(k, vector<int>(k, 0));
for (int num = 1; num <= k; ++num) {
int row = rowPosition[num - 1];
int col = columnPosition[num - 1];
matrix[row][col] = num;
}
return matrix;
}
};