Skip to content

Commit b01295c

Browse files
committed
1349
1 parent 63e85bb commit b01295c

File tree

7 files changed

+314
-0
lines changed

7 files changed

+314
-0
lines changed

leetcode/1349/0.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["#",".",".",".","#"],[".","#",".","#","."],[".",".","#",".","."],[".","#",".","#","."],["#",".",".",".","#"]]

leetcode/1349/2.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[".","#"],["#","#"],["#","."],["#","#"],[".","#"]]

leetcode/1349/51.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[[".",".",".",".","#",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".",".",".",".","#","."],[".",".",".",".",".",".",".","."],[".",".","#",".",".",".",".","."],[".",".",".",".",".",".",".","."],[".",".",".","#",".",".","#","."]]

leetcode/1349/main.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
#include "../../lib/LeetCodeInputTemplate.hpp"
7+
#include "solution-2.cpp"
8+
using namespace std;
9+
10+
typedef vector<int> vi;
11+
typedef vector<vector<int>> vvi;
12+
13+
int main()
14+
{
15+
LeetCodeInput li("51.in");
16+
17+
auto l0 = li.get<vector<vector<char>>>(0);
18+
19+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
20+
Solution solution;
21+
auto output = solution.maxStudents(l0);
22+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
23+
cout << "Time difference = " << chrono::duration_cast<chrono::microseconds> (end - begin).count() << "µs" << endl;
24+
25+
cout << output << endl;
26+
27+
// for (int i: output){
28+
// cout << i << ' ';
29+
// }
30+
31+
// for (auto i: output){
32+
// for (int j: i){
33+
// cout << j << ' ';
34+
// }
35+
// cout << '\n';
36+
// }
37+
38+
return 0;
39+
}

leetcode/1349/solution-1.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime 4 ms Beats 91.1%
7+
Memory 11.5 MB Beats 24.87%
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
/// @brief dp_max_students[i][status of i] = max students to be deployed later
19+
uint8_t dp_max_students[8][8][65536];
20+
/// @brief bit 1 means occupied
21+
uint16_t initial_status[8];
22+
uint8_t m;
23+
uint8_t n;
24+
25+
class Solution {
26+
public:
27+
int maxStudents(vector<vector<char>>& seats) {
28+
m = size(seats);
29+
n = size(seats[0]);
30+
int tmp = 1<<(n+8);
31+
for (int i=0; i<m; ++i){
32+
initial_status[i] = 0;
33+
for (int j=0; j<n; ++j){
34+
initial_status[i] |= (seats[i][j]=='#')<<j;
35+
// fill_n(dp_max_students[i][j], tmp, -1);
36+
memset(dp_max_students[i][j], -1, tmp);
37+
}
38+
}
39+
return dfs_max_students(0, 0, (initial_status[0]<<8) + (initial_status[1]));
40+
}
41+
42+
/// @brief
43+
/// @param i
44+
/// @param j
45+
/// @param status status & 1<<(j+8) is current line, status & 1<<j is next line
46+
/// @return
47+
int dfs_max_students(uint8_t i, uint8_t j, uint16_t status){
48+
if (j>=n){
49+
if (i==m-1){
50+
return 0;
51+
} else {
52+
j = 0;
53+
++i;
54+
status <<= 8;
55+
if (i<m-1){
56+
status |= initial_status[i+1];
57+
}
58+
}
59+
}
60+
61+
if (dp_max_students[i][j][status] <= 64){
62+
return dp_max_students[i][j][status];
63+
}
64+
65+
if ((status & (1 << (j+8))) == 0){
66+
int new_status = status;
67+
if (i<m-1){
68+
if (j) {
69+
new_status |= 1<<j-1;
70+
}
71+
if (j<n-1) {
72+
new_status |= 1<<j+1;
73+
}
74+
}
75+
if (j) {
76+
new_status |= 1<<j+7;
77+
}
78+
if (j<n-1) {
79+
new_status |= 1<<j+9;
80+
}
81+
82+
return dp_max_students[i][j][status] = max(
83+
dfs_max_students(i, j+1, status),
84+
dfs_max_students(i, j+2, new_status) + 1
85+
);
86+
} else {
87+
return dp_max_students[i][j][status] = dfs_max_students(i, j+1, status);
88+
}
89+
}
90+
};
91+
92+
const static auto initialize = [] {
93+
std::ios::sync_with_stdio(false);
94+
std::cin.tie(nullptr);
95+
std::cout.tie(nullptr);
96+
return nullptr;
97+
}();

leetcode/1349/solution-2.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
reference: leetcode fastest solution 0ms
3+
time: O()
4+
space: O()
5+
6+
Runtime
7+
Memory
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
class Solution
19+
{
20+
public:
21+
int maxStudents(vector<vector<char>>& seats)
22+
{
23+
return accumulate(begin(seats), end(seats), 0,
24+
[&](int a, const auto& seat)
25+
{
26+
return a + count(begin(seat), end(seat), '.');
27+
}) -
28+
hungarian(seats);
29+
}
30+
31+
private:
32+
const vector<pair<int, int>> dirs{{-1, -1}, {0, -1}, {1, -1},
33+
{-1, 1}, {0, 1}, {1, 1}};
34+
35+
int hungarian(const vector<vector<char>>& seats)
36+
{
37+
const int m = seats.size();
38+
const int n = seats[0].size();
39+
int count = 0;
40+
vector<vector<int>> seen(m, vector<int>(n));
41+
vector<vector<int>> match(m, vector<int>(n, -1));
42+
43+
for (int i = 0; i < m; ++i)
44+
for (int j = 0; j < n; ++j)
45+
if (seats[i][j] == '.' && match[i][j] == -1)
46+
{
47+
const int sessionId = i * n + j;
48+
seen[i][j] = sessionId;
49+
count += dfs(seats, i, j, sessionId, seen, match);
50+
}
51+
52+
return count;
53+
}
54+
55+
int dfs(const vector<vector<char>>& seats, int i, int j, int sessionId,
56+
vector<vector<int>>& seen, vector<vector<int>>& match)
57+
{
58+
const int m = seats.size();
59+
const int n = seats[0].size();
60+
61+
for (const auto& [dx, dy] : dirs)
62+
{
63+
const int x = i + dx;
64+
const int y = j + dy;
65+
if (x < 0 || x == m || y < 0 || y == n)
66+
continue;
67+
if (seats[x][y] != '.' || seen[x][y] == sessionId)
68+
continue;
69+
seen[x][y] = sessionId;
70+
if (match[x][y] == -1 || dfs(seats, match[x][y] / n, match[x][y] % n,
71+
sessionId, seen, match)) {
72+
match[x][y] = i * n + j;
73+
match[i][j] = x * n + y;
74+
return 1;
75+
}
76+
}
77+
78+
return 0;
79+
}
80+
};

leetcode/1349/solution.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
3+
time: O()
4+
space: O()
5+
6+
Runtime:
7+
Memory Usage:
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
12+
using namespace std;
13+
14+
#define all(x) begin(x), end(x)
15+
typedef vector<int> vi;
16+
typedef vector<vector<int>> vvi;
17+
18+
class Solution {
19+
public:
20+
int m;
21+
int n;
22+
int res = 0;
23+
24+
/// @brief
25+
/// '#', 35, broken
26+
/// '.', 46, unoccupied
27+
/// '*', 42, occupied (extended)
28+
/// <43, unavailable
29+
/// >=43, available
30+
char dp_seats[8][8];
31+
32+
int maxStudents(vector<vector<char>>& seats) {
33+
m = size(seats);
34+
n = size(seats[0]);
35+
for (int i=0; i<m; ++i){
36+
for (int j=0; j<n; ++j){
37+
dp_seats[i][j] = seats[i][j];
38+
}
39+
}
40+
dfs(m-1, n-1, 0);
41+
return res;
42+
}
43+
44+
void dfs(int i, int j, int count){
45+
res = max(res, count);
46+
if (j==-1){
47+
if (i==0){
48+
return;
49+
} else {
50+
j = n-1;
51+
--i;
52+
}
53+
}
54+
char a = 0;
55+
char b = 0;
56+
char c = 0;
57+
char d = 0;
58+
if (dp_seats[i][j] >= 43){
59+
// dp_seats[i][j] = '*';
60+
if (i){
61+
if (j) {
62+
a = dp_seats[i-1][j-1];
63+
dp_seats[i-1][j-1] = '*';
64+
}
65+
if (j<n-1) {
66+
b = dp_seats[i-1][j+1];
67+
dp_seats[i-1][j+1] = '*';
68+
}
69+
}
70+
if (j) {
71+
c = dp_seats[i][j-1];
72+
dp_seats[i][j-1] = '*';
73+
}
74+
if (j<n-1) {
75+
d = dp_seats[i][j+1];
76+
dp_seats[i][j+1] = '*';
77+
}
78+
dfs(i, j-1, count+1);
79+
// dp_seats[i][j] = '.';
80+
if (a) dp_seats[i-1][j-1] = a;
81+
if (b) dp_seats[i-1][j+1] = b;
82+
if (c) dp_seats[i][j-1] = c;
83+
if (d) dp_seats[i][j+1] = d;
84+
}
85+
86+
dfs(i, j-1, count);
87+
}
88+
};
89+
90+
const static auto initialize = [] {
91+
std::ios::sync_with_stdio(false);
92+
std::cin.tie(nullptr);
93+
std::cout.tie(nullptr);
94+
return nullptr;
95+
}();

0 commit comments

Comments
 (0)