-
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
8 changed files
with
134 additions
and
6 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
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,2 @@ | ||
/dual-stack | ||
/single-stack |
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,6 @@ | ||
|
||
dual-stack: dual-stack.cpp | ||
g++ -g dual-stack.cpp -o dual-stack | ||
|
||
single-stack: single-stack.cpp | ||
g++ -g single-stack.cpp -o single-stack |
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,44 @@ | ||
// Copyright (c) 2024 Xu Shaohua <shaohua@biofan.org>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#include <cassert> | ||
|
||
#include <stack> | ||
|
||
class MinStack { | ||
public: | ||
MinStack() {} | ||
|
||
void push(int val) { | ||
this->stack_.push(val); | ||
if (this->min_stack_.empty()) { | ||
this->min_stack_.push(val); | ||
} else { | ||
const int current_min = this->min_stack_.top(); | ||
const int new_min = std::min(current_min, val); | ||
this->min_stack_.push(new_min); | ||
} | ||
} | ||
|
||
void pop() { | ||
this->stack_.pop(); | ||
this->min_stack_.pop(); | ||
} | ||
|
||
int top() { | ||
return this->stack_.top(); | ||
} | ||
|
||
int getMin() { | ||
return this->min_stack_.top(); | ||
} | ||
|
||
private: | ||
std::stack<int> stack_; | ||
std::stack<int> min_stack_; | ||
}; | ||
|
||
int main() { | ||
return 0; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,36 @@ | ||
# 0155. 最小栈 Min Stack | ||
|
||
# | ||
[问题描述](https://leetcode.com/problems/min-stack) | ||
|
||
[问题描述](../problems/) | ||
这个问题可以有两种解法: | ||
|
||
- 使用两个栈, 分别存放正常的数值, 和当前位置的最小值 | ||
- 使用一个栈, 但是栈中每个元素是一个元组, 分别存储 当前值和最小值 | ||
|
||
## 使用两个栈 | ||
|
||
### Rust | ||
|
||
```rust | ||
{{#include src/bin/min-stack-with-dual-vec.rs:5:63}} | ||
``` | ||
|
||
### C++ | ||
|
||
```cpp | ||
{{#include dual-stack.cpp:5:40}} | ||
``` | ||
## 使用一个栈 | ||
### Rust | ||
```rust | ||
{{#include src/bin/min-stack-with-single-vec.rs:5:53}} | ||
``` | ||
|
||
### C++ | ||
|
||
```cpp | ||
{{#include single-stack.cpp:5:}} | ||
``` |
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,42 @@ | ||
// Copyright (c) 2024 Xu Shaohua <shaohua@biofan.org>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#include <cassert> | ||
|
||
#include <stack> | ||
|
||
class MinStack { | ||
public: | ||
MinStack() {} | ||
|
||
void push(int val) { | ||
if (this->stack_.empty()) { | ||
this->stack_.emplace(val, val); | ||
} else { | ||
const int current_min = this->getMin(); | ||
const int new_min = std::min(current_min, val); | ||
this->stack_.emplace(val, new_min); | ||
} | ||
} | ||
|
||
void pop() { | ||
this->stack_.pop(); | ||
} | ||
|
||
int top() { | ||
return this->stack_.top().first; | ||
} | ||
|
||
int getMin() { | ||
return this->stack_.top().second; | ||
} | ||
|
||
private: | ||
std::stack<std::pair<int, int>> stack_; | ||
}; | ||
|
||
int main() { | ||
return 0; | ||
} | ||
|
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