mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2026-01-25 07:34:05 +00:00
day 28: 155-min-stack, 150-evaluate-reverse-polish-notation
This commit is contained in:
14
155-min-stack/driver.cpp
Normal file
14
155-min-stack/driver.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
MinStack *minStack = new MinStack();
|
||||
minStack->push(-2);
|
||||
minStack->push(0);
|
||||
minStack->push(-3);
|
||||
cout << minStack->getMin() << endl; // return -3
|
||||
minStack->pop();
|
||||
cout << minStack->top() << endl; // return 0
|
||||
cout << minStack->getMin() << endl; // return -2
|
||||
return 0;
|
||||
}
|
||||
32
155-min-stack/soln.cpp
Normal file
32
155-min-stack/soln.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
MinStack::MinStack()
|
||||
{
|
||||
_stack = {};
|
||||
total_min = INT_MAX;
|
||||
}
|
||||
|
||||
void MinStack::push(int val)
|
||||
{
|
||||
total_min = min(int(val), total_min);
|
||||
_stack.push_back({val, total_min});
|
||||
}
|
||||
|
||||
void MinStack::pop()
|
||||
{
|
||||
_stack.pop_back();
|
||||
if (_stack.size() == 0)
|
||||
total_min = INT_MAX;
|
||||
else
|
||||
total_min = _stack.back().second;
|
||||
}
|
||||
|
||||
int MinStack::top()
|
||||
{
|
||||
return int(_stack.back().first);
|
||||
}
|
||||
|
||||
int MinStack::getMin()
|
||||
{
|
||||
return int(_stack.back().second);
|
||||
}
|
||||
15
155-min-stack/soln.hpp
Normal file
15
155-min-stack/soln.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class MinStack
|
||||
{
|
||||
private:
|
||||
vector<pair<int, int>> _stack;
|
||||
int total_min;
|
||||
|
||||
public:
|
||||
MinStack();
|
||||
void push(int val);
|
||||
void pop();
|
||||
int top();
|
||||
int getMin();
|
||||
};
|
||||
Reference in New Issue
Block a user