Min Stack
Total Accepted: 15869 Total Submissions: 102810 My Submissions Question Solution
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
getMin() -- Retrieve the minimum element in the stack.
【解題思路】
1、採用輔助包含最小數的棧。
2、每次入棧時,判斷棧頂元素和即將入棧數大小,如果入棧數較小,就存入最小棧。
3、每次出棧時,判斷出棧元素和最小棧棧頂元素是否相等,如果相等,兩個都出棧。
4、這樣就保證最小棧的棧頂元素一定是當前最小元素。
Java AC 298ms
class MinStack {
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> minStack = new Stack<Integer>();
public void push(int x) {
if(minStack.isEmpty() || x <= minStack.peek()){
minStack.push(x);
}
stack.push(x);
}
public void pop() {
int x = stack.pop();
if(x == minStack.peek()){
minStack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
Python AC 232ms
class MinStack:
stack = []
minStack = []
def __init__(self):
self.stack = []
self.minStack = []
# @param x, an integer
# @return an integer
def push(self, x):
if not self.minStack or x <= self.minStack[len(self.minStack) - 1]:
self.minStack.append(x)
self.stack.append(x)
# @return nothing
def pop(self):
x = self.stack.pop()
if x == self.minStack[len(self.minStack) - 1]:
self.minStack.pop()
# @return an integer
def top(self):
return self.stack[len(self.stack) - 1]
# @return an integer
def getMin(self):
return self.minStack[len(self.minStack) - 1]
2、【九度】題目1522:包含min函數的棧時間限制:1 秒內存限制:128 兆特殊判題:否提交:1539解決:493
題目描述:
定義棧的數據結構,請在該類型中實現一個能夠得到棧最小元素的min函數。
輸入:
輸入可能包含多個測試樣例,輸入以EOF結束。
對於每個測試案例,輸入的第一行爲一個整數n(1<=n<=1000000), n代表將要輸入的操作的步驟數。
接下來有n行,每行開始有一個字母Ci。
Ci=’s’時,接下有一個數字k,代表將k壓入棧。
Ci=’o’時,彈出棧頂元素。
輸出:
對應每個測試案例中的每個操作,
若棧不爲空,輸出相應的棧中最小元素。否則,輸出NULL。
樣例輸入:
7
s 3
s 4
s 2
s 1
o
o
s 0
樣例輸出:
3
3
2
1
2
3
0
【階梯思路】
和1一樣。
Java AC
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Stack;
public class Main {
/*
* 1522
*/
public static void main(String[] args) throws Exception {
StreamTokenizer st = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
while (st.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) st.nval;
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> minStack = new Stack<Integer>();
for (int i = 0; i < n; i++) {
st.nextToken();
String s = st.sval;
if (s.equals("s")) {
st.nextToken();
int num = (int) st.nval;
stack.push(num);
if (minStack.isEmpty() || num < minStack.peek()) {
minStack.push(num);
}
} else if (s.equals("o")) {
int num = stack.pop();
if (num == minStack.peek()) {
minStack.pop();
}
}
if (minStack.isEmpty()) {
System.out.println("NULL");
} else {
System.out.println(minStack.peek());
}
}
}
}
}
/**************************************************************
Problem: 1522
User: wzqwsrf
Language: Java
Result: Accepted
Time:860 ms
Memory:26968 kb
****************************************************************/
C++ AC
#include <stdio.h>
#include <stack>
using namespace std;
int n;
char s[1];
int num;
int main(){
while(scanf("%d",&n) != EOF){
stack<int> allStack;
stack<int> minStack;
for (int i = 0; i < n; i++) {
scanf("%s",s);
if (s[0] == 's') {
scanf("%d", &num);
if (minStack.empty() || num <= minStack.top()) {
minStack.push(num);
}
allStack.push(num);
}else if(s[0] == 'o'){
num = allStack.top();
allStack.pop();
if (num == minStack.top()) {
minStack.pop();
}
}
if (minStack.empty()) {
printf("NULL\n");
}else{
printf("%d\n", minStack.top());
}
}
}
}
/**************************************************************
Problem: 1522
User: wzqwsrf
Language: C++
Result: Accepted
Time:20 ms
Memory:1052 kb
****************************************************************/