From 966494c25bdf26af7e4a959a7263eb257b1d6c79 Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Wed, 8 May 2024 21:35:47 +0800 Subject: [PATCH] leetcode: Add 1700 --- .../Cargo.toml | 7 ++ .../index.md | 4 + .../src/main.rs | 76 +++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/leetcode/1700.number-of-students-unable-to-eat-lunch/Cargo.toml create mode 100644 src/leetcode/1700.number-of-students-unable-to-eat-lunch/index.md create mode 100644 src/leetcode/1700.number-of-students-unable-to-eat-lunch/src/main.rs diff --git a/src/leetcode/1700.number-of-students-unable-to-eat-lunch/Cargo.toml b/src/leetcode/1700.number-of-students-unable-to-eat-lunch/Cargo.toml new file mode 100644 index 00000000..d8c5f1d0 --- /dev/null +++ b/src/leetcode/1700.number-of-students-unable-to-eat-lunch/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-1700-number-of-students-unable-to-eat-lunch" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/1700.number-of-students-unable-to-eat-lunch/index.md b/src/leetcode/1700.number-of-students-unable-to-eat-lunch/index.md new file mode 100644 index 00000000..77a618ba --- /dev/null +++ b/src/leetcode/1700.number-of-students-unable-to-eat-lunch/index.md @@ -0,0 +1,4 @@ + +# + +[问题描述](../problems/) diff --git a/src/leetcode/1700.number-of-students-unable-to-eat-lunch/src/main.rs b/src/leetcode/1700.number-of-students-unable-to-eat-lunch/src/main.rs new file mode 100644 index 00000000..817c89a4 --- /dev/null +++ b/src/leetcode/1700.number-of-students-unable-to-eat-lunch/src/main.rs @@ -0,0 +1,76 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +use std::collections::VecDeque; + +// Stack +pub fn count_students1(students: Vec, sandwiches: Vec) -> i32 { + assert_eq!(students.len(), sandwiches.len()); + assert!(!students.is_empty()); + + let mut queue: VecDeque = VecDeque::new(); + let mut sandwiches = sandwiches; + sandwiches.reverse(); + + // 遍历所有的学生. + for student in students { + if Some(&student) == sandwiches.last() { + // 找到了喜欢吃的, 直接离开 + let _ = sandwiches.pop(); + } else { + // 入队列. + queue.push_back(student); + } + } + + let mut sandwich_count = sandwiches.len(); + let mut index = 0; + while let Some(student) = queue.pop_front() { + if Some(&student) == sandwiches.last() { + // 找到了合适的, 走吧. + let _ = sandwiches.pop(); + } else { + // 继续入队列. + queue.push_back(student); + } + index += 1; + // 循环一圈之后, 三明治没有减少. + if index > sandwich_count { + if sandwich_count == sandwiches.len() { + break; + } else { + // 进入下一次循环 + index = 0; + sandwich_count = sandwiches.len(); + } + } + } + queue.len() as i32 +} + +pub type SolutionFn = fn(Vec, Vec) -> i32; + +fn check_solution(func: SolutionFn) { + let students = vec![1, 1, 0, 0]; + let sandwiches = vec![0, 1, 0, 1]; + assert_eq!(func(students, sandwiches), 0); + + let students = vec![1, 1, 1, 0, 0, 1]; + let sandwiches = vec![1, 0, 0, 0, 1, 1]; + assert_eq!(func(students, sandwiches), 3); +} + +fn main() { + check_solution(count_students1); +} + +#[cfg(test)] +mod tests { + use super::{check_solution, count_students1}; + + #[test] + fn test_count_students1() { + check_solution(count_students1); + } +}