-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 参加考试的最大学生数 | ||
// https://leetcode.cn/problems/maximum-students-taking-exam | ||
// INLINE ../../images/math/maximum_students_taking_exam.jpeg | ||
|
||
#include <headers.hpp> | ||
|
||
class Solution { | ||
public: | ||
int maxStudents(vector<vector<char>> &seats) { | ||
int m = seats.size(); | ||
int n = seats[0].size(); | ||
vector<int> ss(m); | ||
vector<vector<int>> f(1 << n, vector<int>(m, -1)); | ||
for (int i = 0; i < m; ++i) { | ||
for (int j = 0; j < n; ++j) { | ||
if (seats[i][j] == '.') { | ||
ss[i] |= 1 << j; | ||
} | ||
} | ||
} | ||
function<int(int, int)> dfs = [&](int seat, int i) -> int { | ||
if (f[seat][i] != -1) { | ||
return f[seat][i]; | ||
} | ||
int ans = 0; | ||
for (int mask = 0; mask < 1 << n; ++mask) { | ||
if ((seat | mask) != seat || (mask & (mask << 1)) != 0) { | ||
continue; | ||
} | ||
int cnt = __builtin_popcount(mask); | ||
if (i == m - 1) { | ||
ans = max(ans, cnt); | ||
} else { | ||
int nxt = ss[i + 1]; | ||
nxt &= ~(mask >> 1); | ||
nxt &= ~(mask << 1); | ||
ans = max(ans, cnt + dfs(nxt, i + 1)); | ||
} | ||
} | ||
return f[seat][i] = ans; | ||
}; | ||
return dfs(ss[0], 0); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 执行编译时间:2023-12-25 10:21:30 | ||
// 执行编译时间:2023-12-26 10:04:16 | ||
#include <gtest/gtest.h> | ||
#include <lib.hpp> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <math/maximum_students_taking_exam.cpp> | ||
|
||
TEST(参加考试的最大学生数, maxStudents) { | ||
Solution solution; | ||
// 示例 1: | ||
// 输入:seats = [["#",".","#","#",".","#"], | ||
// [".","#","#","#","#","."], | ||
// ["#",".","#","#",".","#"]] | ||
// 输出:4 | ||
// 解释:教师可以让 4 个学生坐在可用的座位上,这样他们就无法在考试中作弊。 | ||
vector<vector<char>> seats = {{'#', '.', '#', '#', '.', '#'}, | ||
{'.', '#', '#', '#', '#', '.'}, | ||
{'#', '.', '#', '#', '.', '#'}}; | ||
EXPECT_EQ(solution.maxStudents(seats), 4); | ||
|
||
// 示例 2: | ||
// 输入:seats = [[".","#"], | ||
// ["#","#"], | ||
// ["#","."], | ||
// ["#","#"], | ||
// [".","#"]] | ||
// 输出:3 | ||
// 解释:让所有学生坐在可用的座位上。 | ||
seats = {{'.', '#'}, {'#', '#'}, {'#', '.'}, {'#', '#'}, {'.', '#'}}; | ||
EXPECT_EQ(solution.maxStudents(seats), 3); | ||
|
||
// 示例 3: | ||
// 输入:seats = [["#",".",".",".","#"], | ||
// [".","#",".","#","."], | ||
// [".",".","#",".","."], | ||
// [".","#",".","#","."], | ||
// ["#",".",".",".","#"]] | ||
// 输出:10 | ||
// 解释:让学生坐在第 1、3 和 5 列的可用座位上。 | ||
seats = {{'#', '.', '.', '.', '#'}, | ||
{'.', '#', '.', '#', '.'}, | ||
{'.', '.', '#', '.', '.'}, | ||
{'.', '#', '.', '#', '.'}, | ||
{'#', '.', '.', '.', '#'}}; | ||
EXPECT_EQ(solution.maxStudents(seats), 10); | ||
} |