-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
111 additions
and
0 deletions.
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,54 @@ | ||
// 参加考试的最大学生数 | ||
// https://leetcode.cn/problems/maximum-students-taking-exam | ||
// INLINE ../../images/math/maximum_students_taking_exam.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn max_students(seats: Vec<Vec<char>>) -> i32 { | ||
fn dfs( | ||
seat: usize, | ||
i: usize, | ||
f: &mut Vec<Vec<i32>>, | ||
ss: &Vec<usize>, | ||
m: usize, | ||
n: usize, | ||
) -> i32 { | ||
if f[seat][i] != -1 { | ||
return f[seat][i]; | ||
} | ||
let mut ans = 0; | ||
for mask in 0..1 << n { | ||
if (seat | mask) != seat || (mask & (mask << 1)) != 0 { | ||
continue; | ||
} | ||
let cnt = mask.count_ones(); | ||
if i == m - 1 { | ||
ans = std::cmp::max(ans, cnt as i32); | ||
} else { | ||
let mut nxt = ss[i + 1]; | ||
nxt &= !(mask >> 1); | ||
nxt &= !(mask << 1); | ||
ans = std::cmp::max(ans, cnt as i32 + dfs(nxt, i + 1, f, ss, m, n)); | ||
} | ||
} | ||
f[seat][i] = ans; | ||
ans | ||
} | ||
|
||
let m = seats.len(); | ||
let n = seats[0].len(); | ||
let mut ss = vec![0; m]; | ||
let mut f = vec![vec![-1; m]; 1 << n]; | ||
|
||
for i in 0..m { | ||
for j in 0..n { | ||
if seats[i][j] == '.' { | ||
ss[i] |= 1 << j; | ||
} | ||
} | ||
} | ||
|
||
dfs(ss[0], 0, &mut f, &ss, m, n) | ||
} | ||
} |
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
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,51 @@ | ||
use rust_practice::math::maximum_students_taking_exam::Solution; | ||
|
||
#[test] | ||
fn max_students() { | ||
// 示例 1: | ||
// 输入:seats = [["#",".","#","#",".","#"], | ||
// [".","#","#","#","#","."], | ||
// ["#",".","#","#",".","#"]] | ||
// 输出:4 | ||
// 解释:教师可以让 4 个学生坐在可用的座位上,这样他们就无法在考试中作弊。 | ||
let seats = vec![ | ||
vec!['#', '.', '#', '#', '.', '#'], | ||
vec!['.', '#', '#', '#', '#', '.'], | ||
vec!['#', '.', '#', '#', '.', '#'], | ||
]; | ||
assert_eq!(Solution::max_students(seats), 4); | ||
|
||
// 示例 2: | ||
// 输入:seats = [[".","#"], | ||
// ["#","#"], | ||
// ["#","."], | ||
// ["#","#"], | ||
// [".","#"]] | ||
// 输出:3 | ||
// 解释:让所有学生坐在可用的座位上。 | ||
let seats = vec![ | ||
vec!['.', '#'], | ||
vec!['#', '#'], | ||
vec!['#', '.'], | ||
vec!['#', '#'], | ||
vec!['.', '#'], | ||
]; | ||
assert_eq!(Solution::max_students(seats), 3); | ||
|
||
// 示例 3: | ||
// 输入:seats = [["#",".",".",".","#"], | ||
// [".","#",".","#","."], | ||
// [".",".","#",".","."], | ||
// [".","#",".","#","."], | ||
// ["#",".",".",".","#"]] | ||
// 输出:10 | ||
// 解释:让学生坐在第 1、3 和 5 列的可用座位上。 | ||
let seats = vec![ | ||
vec!['#', '.', '.', '.', '#'], | ||
vec!['.', '#', '.', '#', '.'], | ||
vec!['.', '.', '#', '.', '.'], | ||
vec!['.', '#', '.', '#', '.'], | ||
vec!['#', '.', '.', '.', '#'], | ||
]; | ||
assert_eq!(Solution::max_students(seats), 10); | ||
} |
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