Skip to content

Commit

Permalink
add: 参加考试的最大学生数
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Dec 26, 2023
1 parent 3ffbd27 commit a604771
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,7 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文
- [不浪费原料的汉堡制作方案](src/math/number_of_burgers_with_no_waste_of_ingredients.cpp) [数学]

- LeetCode 1276. 不浪费原料的汉堡制作方案 <https://leetcode.cn/problems/number-of-burgers-with-no-waste-of-ingredients>

- [参加考试的最大学生数](src/math/maximum_students_taking_exam.cpp) [位运算, 数组, 动态规划, 状态压缩, 矩阵]

- LeetCode 1349. 参加考试的最大学生数 <https://leetcode.cn/problems/maximum-students-taking-exam>
Binary file added images/math/maximum_students_taking_exam.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions src/math/maximum_students_taking_exam.cpp
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);
}
};
2 changes: 1 addition & 1 deletion test/lib/lib_test.cpp
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>

Expand Down
41 changes: 41 additions & 0 deletions test/math/maximum_students_taking_exam_test.cpp
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);
}

0 comments on commit a604771

Please sign in to comment.