https://leetcode.com/problems/min-stack/ #include <vector> #include <queue> #include <map> #include <iostream> using namespace std; class MinStack { public: vector<int> vec; priority_queue<int,vector<int>,greater<…
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } void push(int x) { if(s1.empty() && s2.empty()){ s1.push(x); s2.push(x); } else{ if(x < s2.top()){ s1.push(x); s2.push(x); } else{ s1.push(x); s2…
翻译 设计支持push.pop.top和在常量时间内检索最小元素的栈. push(x) -- 推送元素X进栈 pop() -- 移除栈顶元素 top() -- 得到栈顶元素 getMin() -- 检索栈的最小元素 原文 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop…
题目描述: 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 min…
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 e…
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 e…
题意:设计一个能输出栈内最小值的栈 该题设计两个栈,一个栈是正常的栈s,而另一个是存最小值的栈sm 在push时要判断sm是否为空,如果为空或者非空但是栈顶元素大于等于插入值的 需要在sm中插入x 同样地在pop时,s的元素被删除了,那么sm中的也应该被删除. 通过这些操作维护sm能很巧妙在O(1)复杂度得到最小值. class MinStack { public: stack<int> sm; stack<int> s; void push(int x) { s.push(x);…
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…
https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益. 分析 贪心的取,当然是低买高卖.当买卖的顺序需要斟酌.考虑用小顶堆(优先队列)来维护这过程,我们每次得到一个新的价格,将其和堆顶的价格比较,如果比堆顶的价格低,就直接放入堆中,如果比堆顶的价格高,就意味着我们可以提前以堆顶的价格买入一个物品,然后以当前价格卖出,因此我们可以算出本次收益加到总收益…
vector<int> getLeastNumber(vector<int>& arr,int k){ vector<int> vec(k,); if(==k) return vec; priority_queue<int> q; for(int i = ;i < k;i++){ q.push(arr[i]); } for(int i = k;i <(int)arr.size();i++){ if(q.top()>arr[i]){…