From e066fd1c005e9a481b8d3913e09b9f3da216920c Mon Sep 17 00:00:00 2001 From: Xu Shaohua Date: Wed, 8 May 2024 08:31:28 +0800 Subject: [PATCH] leetcode: Add 0155 --- src/leetcode/0155.min-stack/Cargo.toml | 7 ++ src/leetcode/0155.min-stack/index.md | 4 + src/leetcode/0155.min-stack/src/bin/double.rs | 87 +++++++++++++++++++ src/leetcode/0155.min-stack/src/bin/single.rs | 78 +++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 src/leetcode/0155.min-stack/Cargo.toml create mode 100644 src/leetcode/0155.min-stack/index.md create mode 100644 src/leetcode/0155.min-stack/src/bin/double.rs create mode 100644 src/leetcode/0155.min-stack/src/bin/single.rs diff --git a/src/leetcode/0155.min-stack/Cargo.toml b/src/leetcode/0155.min-stack/Cargo.toml new file mode 100644 index 00000000..300a5b69 --- /dev/null +++ b/src/leetcode/0155.min-stack/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-0155-min-stack" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/0155.min-stack/index.md b/src/leetcode/0155.min-stack/index.md new file mode 100644 index 00000000..77a618ba --- /dev/null +++ b/src/leetcode/0155.min-stack/index.md @@ -0,0 +1,4 @@ + +# + +[问题描述](../problems/) diff --git a/src/leetcode/0155.min-stack/src/bin/double.rs b/src/leetcode/0155.min-stack/src/bin/double.rs new file mode 100644 index 00000000..aeeb71f9 --- /dev/null +++ b/src/leetcode/0155.min-stack/src/bin/double.rs @@ -0,0 +1,87 @@ +// 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. + +// 用两个栈来实现 + +#[derive(Debug, Clone)] +pub struct MinStack { + // 正常的栈, 按入栈顺序存储. + stack: Vec, + // 最小栈, 里面存储当前位置的最小元素, 最小元素可能是有重复的, + // 其长度与 `stack` 相同. + min_stack: Vec, +} + +impl Default for MinStack { + fn default() -> Self { + Self::new() + } +} + +impl MinStack { + #[must_use] + #[inline] + pub const fn new() -> Self { + Self { + stack: Vec::new(), + + min_stack: Vec::new(), + } + } + + pub fn push(&mut self, val: i32) { + self.stack.push(val); + if let Some(top) = self.min_stack.last() { + self.min_stack.push(*top.min(&val)); + } else { + self.min_stack.push(val); + } + } + + pub fn pop(&mut self) { + let _ = self.stack.pop(); + let _ = self.min_stack.pop(); + } + + #[must_use] + pub fn top(&self) -> i32 { + self.stack.last().copied().unwrap_or_default() + } + + #[must_use] + pub fn get_min(&self) -> i32 { + self.min_stack.last().copied().unwrap_or_default() + } + + #[must_use] + #[inline] + pub fn is_empty(&self) -> bool { + self.stack.is_empty() + } +} + +fn check_solution() { + let mut obj = MinStack::new(); + obj.push(-2); + obj.push(0); + obj.push(-3); + assert_eq!(obj.get_min(), -3); + obj.pop(); + assert_eq!(obj.top(), 0); + assert_eq!(obj.get_min(), -2); +} + +fn main() { + check_solution(); +} + +#[cfg(test)] +mod tests { + use super::check_solution; + + #[test] + fn test_min_stack() { + check_solution(); + } +} diff --git a/src/leetcode/0155.min-stack/src/bin/single.rs b/src/leetcode/0155.min-stack/src/bin/single.rs new file mode 100644 index 00000000..6a0c9e81 --- /dev/null +++ b/src/leetcode/0155.min-stack/src/bin/single.rs @@ -0,0 +1,78 @@ +// 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. + +// 用一个栈来实现 +#[derive(Debug, Clone)] +pub struct MinStack { + // 元组: (当前的元素, 当前最小的元素) + stack: Vec<(i32, i32)>, +} + +impl Default for MinStack { + fn default() -> Self { + Self::new() + } +} + +impl MinStack { + #[must_use] + #[inline] + pub const fn new() -> Self { + Self { stack: Vec::new() } + } + + pub fn push(&mut self, val: i32) { + if let Some(top) = self.stack.last() { + let min = top.1.min(val); + self.stack.push((val, min)); + } else { + self.stack.push((val, val)); + } + } + + pub fn pop(&mut self) { + let _ = self.stack.pop().unwrap(); + } + + #[must_use] + pub fn top(&self) -> i32 { + self.stack.last().unwrap().0 + } + + #[must_use] + pub fn get_min(&self) -> i32 { + self.stack.last().unwrap().1 + } + + #[must_use] + #[inline] + pub fn is_empty(&self) -> bool { + self.stack.is_empty() + } +} + +fn check_solution() { + let mut obj = MinStack::new(); + obj.push(-2); + obj.push(0); + obj.push(-3); + assert_eq!(obj.get_min(), -3); + obj.pop(); + assert_eq!(obj.top(), 0); + assert_eq!(obj.get_min(), -2); +} + +fn main() { + check_solution(); +} + +#[cfg(test)] +mod tests { + use super::check_solution; + + #[test] + fn test_min_stack() { + check_solution(); + } +}