forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum-students-taking-exam.cpp
322 lines (289 loc) · 9.92 KB
/
maximum-students-taking-exam.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Time: O(m * n * sqrt(m * n))
// Space: O(m * n)
// template from https://www.geeksforgeeks.org/hopcroft-karp-algorithm-for-maximum-matching-set-2-implementation/
static const int NIL = 0;
static const int INF = numeric_limits<int>::max();
// A class to represent Bipartite graph for Hopcroft
// Karp implementation
// Time: O(E * sqrt(V))
// Space: O(V)
class BipGraph
{
// m and n are number of vertices on left
// and right sides of Bipartite Graph
int m, n;
// adj[u] stores adjacents of left side
// vertex 'u'. The value of u ranges from 1 to m.
// 0 is used for dummy vertex
list<int> *adj;
// These are basically pointers to arrays needed
// for hopcroftKarp()
int *pairU, *pairV, *dist;
public:
BipGraph(int m, int n); // Constructor
void addEdge(int u, int v); // To add edge
// Returns true if there is an augmenting path
bool bfs();
// Adds augmenting path if there is one beginning
// with u
bool dfs(int u);
// Returns size of maximum matcing
int hopcroftKarp();
};
// Returns size of maximum matching
int BipGraph::hopcroftKarp()
{
// pairU[u] stores pair of u in matching where u
// is a vertex on left side of Bipartite Graph.
// If u doesn't have any pair, then pairU[u] is NIL
pairU = new int[m+1];
// pairV[v] stores pair of v in matching. If v
// doesn't have any pair, then pairU[v] is NIL
pairV = new int[n+1];
// dist[u] stores distance of left side vertices
// dist[u] is one more than dist[u'] if u is next
// to u'in augmenting path
dist = new int[m+1];
// Initialize NIL as pair of all vertices
for (int u=0; u<m; u++)
pairU[u] = NIL;
for (int v=0; v<n; v++)
pairV[v] = NIL;
// Initialize result
int result = 0;
// Keep updating the result while there is an
// augmenting path.
while (bfs())
{
// Find a free vertex
for (int u=1; u<=m; u++)
// If current vertex is free and there is
// an augmenting path from current vertex
if (pairU[u]==NIL && dfs(u))
result++;
}
return result;
}
// Returns true if there is an augmenting path, else returns
// false
bool BipGraph::bfs()
{
queue<int> Q; //an integer queue
// First layer of vertices (set distance as 0)
for (int u=1; u<=m; u++)
{
// If this is a free vertex, add it to queue
if (pairU[u]==NIL)
{
// u is not matched
dist[u] = 0;
Q.push(u);
}
// Else set distance as infinite so that this vertex
// is considered next time
else dist[u] = INF;
}
// Initialize distance to NIL as infinite
dist[NIL] = INF;
// Q is going to contain vertices of left side only.
while (!Q.empty())
{
// Dequeue a vertex
int u = Q.front();
Q.pop();
// If this node is not NIL and can provide a shorter path to NIL
if (dist[u] < dist[NIL])
{
// Get all adjacent vertices of the dequeued vertex u
list<int>::iterator i;
for (i=adj[u].begin(); i!=adj[u].end(); ++i)
{
int v = *i;
// If pair of v is not considered so far
// (v, pairV[V]) is not yet explored edge.
if (dist[pairV[v]] == INF)
{
// Consider the pair and add it to queue
dist[pairV[v]] = dist[u] + 1;
Q.push(pairV[v]);
}
}
}
}
// If we could come back to NIL using alternating path of distinct
// vertices then there is an augmenting path
return (dist[NIL] != INF);
}
// Returns true if there is an augmenting path beginning with free vertex u
bool BipGraph::dfs(int u)
{
if (u != NIL)
{
list<int>::iterator i;
for (i=adj[u].begin(); i!=adj[u].end(); ++i)
{
// Adjacent to u
int v = *i;
// Follow the distances set by BFS
if (dist[pairV[v]] == dist[u]+1)
{
// If dfs for pair of v also returns
// true
if (dfs(pairV[v]) == true)
{
pairV[v] = u;
pairU[u] = v;
return true;
}
}
}
// If there is no augmenting path beginning with u.
dist[u] = INF;
return false;
}
return true;
}
// Constructor
BipGraph::BipGraph(int m, int n)
{
this->m = m;
this->n = n;
adj = new list<int>[m+1];
}
// To add edge from u to v and v to u
void BipGraph::addEdge(int u, int v)
{
adj[u].push_back(v); // Add u to v’s list.
}
// Hopcroft-Karp bipartite matching
class Solution {
public:
int maxStudents(vector<vector<char>>& seats) {
static vector<pair<int, int>> directions = {{-1, -1}, {0, -1}, {1, -1},
{-1, 1}, {0, 1}, {1, 1}};
unordered_map<int, int> lookup;
int u = 0, v = 0;
for (int i = 0; i < seats.size(); ++i) {
for (int j = 0; j < seats[0].size(); ++j) {
if (seats[i][j] != '.') {
continue;
}
lookup[i * seats[0].size() + j] = (j % 2 == 0) ? ++u : ++v;
}
}
BipGraph g(seats.size() * seats[0].size(), seats.size() * seats[0].size());
for (int i = 0; i < seats.size(); ++i) {
for (int j = 0; j < seats[0].size(); j += 2) {
if (seats[i][j] != '.') {
continue;
}
for (const auto& [dx, dy] : directions) {
const auto& [ni, nj] = make_pair(i + dx, j + dy);
if (0 <= ni && ni < seats.size() &&
0 <= nj && nj < seats[0].size() &&
seats[ni][nj] == '.') {
g.addEdge(lookup[i * seats[0].size() + j],
lookup[ni * seats[0].size() + nj]);
}
}
}
}
return u + v - g.hopcroftKarp();
}
};
// Time: O(|V| * |E|) = O(m^2 * n^2)
// Space: O(|V| + |E|) = O(m * n)
// Hungarian bipartite matching
class Solution2 {
public:
int maxStudents(vector<vector<char>>& seats) {
int count = 0;
for (int i = 0; i < seats.size(); ++i) {
for (int j = 0; j < seats[0].size(); ++j) {
if (seats[i][j] != '.') {
continue;
}
++count;
}
}
return count - Hungarian(seats);
}
private:
int Hungarian(const vector<vector<char>>& seats) {
int result = 0;
vector<vector<pair<int, int>>> matching(seats.size(),
vector<pair<int, int>>(seats[0].size(), {-1, -1}));
for (int i = 0; i < seats.size(); ++i) {
for (int j = 0; j < seats[0].size(); j += 2) {
if (seats[i][j] != '.') {
continue;
}
vector<vector<bool>> lookup(seats.size(),
vector<bool>(seats[0].size(), false));
if (dfs(seats, {i, j}, &lookup, &matching)) {
++result;
}
}
}
return result;
}
int dfs(const vector<vector<char>>& seats,
const pair<int, int>& e,
vector<vector<bool>> *lookup,
vector<vector<pair<int, int>>> *matching) {
static vector<pair<int, int>> directions = {{-1, -1}, {0, -1}, {1, -1},
{-1, 1}, {0, 1}, {1, 1}};
const auto& [i, j] = e;
for (const auto& [dx, dy] : directions) {
const auto& [ni, nj] = make_pair(i + dx, j + dy);
if (0 <= ni && ni < seats.size() &&
0 <= nj && nj < seats[0].size() &&
seats[ni][nj] == '.' &&
!(*lookup)[ni][nj]) {
(*lookup)[ni][nj] = true;
if ((*matching)[ni][nj].first == -1 ||
dfs(seats, (*matching)[ni][nj], lookup, matching)) {
(*matching)[ni][nj] = e;
return true;
}
}
}
return false;
}
};
// Time: O(m * 2^n * 2^n) = O(m * 4^n)
// Space: O(2^n)
// dp solution
class Solution3 {
public:
int maxStudents(vector<vector<char>>& seats) {
const int state_size = 1 << seats[0].size();
unordered_map<int, int> dp;
dp[0] = 0;
for (const auto& row : seats) {
int invalid_mask = 0;
for (int i = 0; i < row.size(); ++i) {
if (row[i] == '#') {
invalid_mask |= 1 << i;
}
}
unordered_map<int, int> new_dp;
for (const auto& [mask1, v1] : dp) {
for (int mask2 = 0; mask2 < state_size; ++mask2) {
if ((mask2 & invalid_mask) ||
(mask2 & (mask1 << 1)) || (mask2 & (mask1 >> 1)) ||
(mask2 & (mask2 << 1)) || (mask2 & (mask2 >> 1))) {
continue;
}
new_dp[mask2] = max(new_dp.count(mask2) ? new_dp[mask2] : 0,
v1 + __builtin_popcount(mask2));
}
}
dp = move(new_dp);
}
return dp.empty() ? 0 : max_element(dp.cbegin(), dp.cend(),
[](const auto& a, const auto& b) {
return a.second < b.second;
})->second;
}
};