Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8b28ec3

Browse files
committed
solve #155
1 parent fce0aa7 commit 8b28ec3

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,4 @@ mod n0151_reverse_words_in_a_string;
149149
mod n0152_maximum_product_subarray;
150150
mod n0153_find_minimum_in_rotated_sorted_array;
151151
mod n0154_find_minimum_in_rotated_sorted_array_ii;
152+
mod n0155_min_stack;

src/n0155_min_stack.rs

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* [155] Min Stack
3+
*
4+
*
5+
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
6+
*
7+
*
8+
* push(x) -- Push element x onto stack.
9+
*
10+
*
11+
* pop() -- Removes the element on top of the stack.
12+
*
13+
*
14+
* top() -- Get the top element.
15+
*
16+
*
17+
* getMin() -- Retrieve the minimum element in the stack.
18+
*
19+
*
20+
*
21+
*
22+
* Example:<br />
23+
*
24+
* MinStack minStack = new MinStack();
25+
* minStack.push(-2);
26+
* minStack.push(0);
27+
* minStack.push(-3);
28+
* minStack.getMin(); --> Returns -3.
29+
* minStack.pop();
30+
* minStack.top(); --> Returns 0.
31+
* minStack.getMin(); --> Returns -2.
32+
*
33+
*
34+
*/
35+
pub struct Solution {}
36+
37+
// submission codes start here
38+
39+
/*
40+
这题居然是 easy... 我怀疑人生了, getMin() 怎么能做到常数时间? Heap 也是 LogN 啊
41+
42+
看了最高票解之后...........天哪, 我可太菜了
43+
44+
核心思想是保证每次 pop 时都能以常数时间更新最小值, 这就需要在空间上以某种方式记录下来
45+
46+
那一种做法就是存储每个元素和最小值之间的差值, 这样 pop 的时候就能不断还原出原始值
47+
48+
另一种更直观的做法就是每次入栈 min 时, 都把前一个 min (当前第二小的数字) 放在它前面, 作为记录
49+
*/
50+
struct MinStack {
51+
vec: Vec<i32>,
52+
min: i32,
53+
}
54+
55+
impl MinStack {
56+
57+
/** initialize your data structure here. */
58+
pub fn new() -> Self {
59+
MinStack{
60+
vec: Vec::new(),
61+
min: i32::max_value(),
62+
}
63+
}
64+
65+
pub fn push(&mut self, x: i32) {
66+
if x <= self.min {
67+
self.vec.push(self.min);
68+
self.min = x;
69+
}
70+
self.vec.push(x);
71+
}
72+
73+
pub fn pop(&mut self) {
74+
if self.vec.pop().unwrap() == self.min {
75+
self.min = self.vec.pop().unwrap();
76+
}
77+
}
78+
79+
pub fn top(&self) -> i32 {
80+
*self.vec.last().unwrap()
81+
}
82+
83+
pub fn get_min(&self) -> i32 {
84+
self.min
85+
}
86+
}
87+
88+
/**
89+
* Your MinStack object will be instantiated and called as such:
90+
* let obj = MinStack::new();
91+
* obj.push(x);
92+
* obj.pop();
93+
* let ret_3: i32 = obj.top();
94+
* let ret_4: i32 = obj.get_min();
95+
*/
96+
97+
// submission codes end
98+
99+
#[cfg(test)]
100+
mod tests {
101+
use super::*;
102+
103+
#[test]
104+
pub fn test_155() {
105+
let mut min_stack = MinStack::new();
106+
min_stack.push(-2);
107+
min_stack.push(0);
108+
min_stack.push(-3);
109+
assert_eq!(min_stack.get_min(), -3); // --> Returns -3.
110+
min_stack.pop();
111+
assert_eq!(min_stack.top(), 0); // --> Returns 0.
112+
assert_eq!(min_stack.get_min(), -2); // --> Returns -2.[]
113+
}
114+
}

0 commit comments

Comments
 (0)