-
Notifications
You must be signed in to change notification settings - Fork 1
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
4 changed files
with
176 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-0155-min-stack" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
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,4 @@ | ||
|
||
# | ||
|
||
[问题描述](../problems/) |
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,87 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. 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>, | ||
// 最小栈, 里面存储当前位置的最小元素, 最小元素可能是有重复的, | ||
// 其长度与 `stack` 相同. | ||
min_stack: Vec<i32>, | ||
} | ||
|
||
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(); | ||
} | ||
} |
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,78 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. 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(); | ||
} | ||
} |