标签:des style blog http io color ar os sp
一共用两个栈。一个用来放数据,另一个专门用来存放当前最小值。
1 class MinStack { 2 public: 3 void push(int x) { 4 elements.push(x); 5 if (mins.empty()||x<=mins.top()) 6 mins.push(x); 7 } 8 9 void pop() { 10 if (elements.empty()) return; 11 if (elements.top() == mins.top()) 12 mins.pop(); 13 elements.pop(); 14 } 15 16 int top() { 17 return elements.top(); 18 } 19 20 int getMin() { 21 return mins.top(); 22 } 23 private: 24 stack<int> elements; 25 stack<int> mins; 26 };
==================================================================================
http://leetcode.com/2010/11/stack-that-supports-push-pop-and-getmin.html
Design a stack that supports push, pop, and retrieving the minimum element in constant time. Can you do this?
Today, I read this interesting problem (exercise 4-44) from the excellent book “The Algorithm Design Manual” by Steven Skiena.
Initially I thought of using an extra min-heap to store the elements. Although this enables us to retrieve the minimum element in O(1), push and pop operations both operates in O(lg N), where N is the total number of elements in the stack. Definitely not a desirable method.
How about recording the current minimum in the stack? This works until you pop current minimum off the stack, where you would have to update your next minimum, which takes O(N) time to examine all elements.
Hint:
Assume you have an extra stack. What would you do with the extra stack?
Solution:
The solution is surprisingly simple and elegant — Use an extra stack to maintain the minimums. What does this mean?
All of the above operates in O(1) constant time.
There is one small gotcha I purposely left in the above solution. Could you figure it out without looking at the code below first?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
struct StackGetMin {
void push(int x) {
elements.push(x);
if (minStack.empty() || x <= minStack.top())
minStack.push(x);
}
bool pop() {
if (elements.empty()) return false;
if (elements.top() == minStack.top())
minStack.pop();
elements.pop();
return true;
}
bool getMin(int &min) {
if (minStack.empty()) {
return false;
} else {
min = minStack.top();
return true;
}
}
stack<int> elements;
stack<int> minStack;
};
|
Before we conclude this section, try a variation of this problem (appeared as a Google interview question):
Design a queue that supports push_rear, pop_front, and get_min in O(1). Would that be elegantly possible too?
Hint:
Use two queues.
If you need extra hints, see this post (both problems are related). If you could design a queue with push_rear, pop_front, and get_min in O(1), then finding the Sliding Window Maximum could be solved by direct application of such data structure.
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/forcheryl/p/4087760.html