Skip to content

Commit

Permalink
leetcode: Add 0155
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed May 8, 2024
1 parent 8920935 commit e066fd1
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/leetcode/0155.min-stack/Cargo.toml
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]
4 changes: 4 additions & 0 deletions src/leetcode/0155.min-stack/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#

[问题描述](../problems/)
87 changes: 87 additions & 0 deletions src/leetcode/0155.min-stack/src/bin/double.rs
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();
}
}
78 changes: 78 additions & 0 deletions src/leetcode/0155.min-stack/src/bin/single.rs
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();
}
}

0 comments on commit e066fd1

Please sign in to comment.