From 392b6c97e91e93838b7fa7bcefa2264dce281def Mon Sep 17 00:00:00 2001 From: Kaushik Narayan R Date: Sun, 29 Oct 2023 19:14:36 -0700 Subject: [PATCH] day 28: 155-min-stack, 150-evaluate-reverse-polish-notation --- .../driver.cpp | 10 +++++ 150-evaluate-reverse-polish-notation/soln.cpp | 42 +++++++++++++++++++ 150-evaluate-reverse-polish-notation/soln.hpp | 8 ++++ 155-min-stack/driver.cpp | 14 +++++++ 155-min-stack/soln.cpp | 32 ++++++++++++++ 155-min-stack/soln.hpp | 15 +++++++ 6 files changed, 121 insertions(+) create mode 100644 150-evaluate-reverse-polish-notation/driver.cpp create mode 100644 150-evaluate-reverse-polish-notation/soln.cpp create mode 100644 150-evaluate-reverse-polish-notation/soln.hpp create mode 100644 155-min-stack/driver.cpp create mode 100644 155-min-stack/soln.cpp create mode 100644 155-min-stack/soln.hpp diff --git a/150-evaluate-reverse-polish-notation/driver.cpp b/150-evaluate-reverse-polish-notation/driver.cpp new file mode 100644 index 0000000..996f239 --- /dev/null +++ b/150-evaluate-reverse-polish-notation/driver.cpp @@ -0,0 +1,10 @@ +#include "soln.hpp" + +int main() +{ + vector tokens{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"}; + int answer = 22; + Solution soln; + cout << "Evaluated postfix expression correctly? " << soln.test(tokens, answer) << endl; + return 0; +} \ No newline at end of file diff --git a/150-evaluate-reverse-polish-notation/soln.cpp b/150-evaluate-reverse-polish-notation/soln.cpp new file mode 100644 index 0000000..d1072e2 --- /dev/null +++ b/150-evaluate-reverse-polish-notation/soln.cpp @@ -0,0 +1,42 @@ +#include "soln.hpp" + +int Solution::evalRPN(vector &tokens) +{ + vector operands; + int a, b; + for (int i = 0; i < tokens.size(); i++) + { + if (tokens[i].length() > 1 || isdigit(tokens[i][0])) + { + operands.push_back(stoi(tokens[i])); + } + else + { + b = operands.back(); + operands.pop_back(); + a = operands.back(); + operands.pop_back(); + switch (tokens[i][0]) + { + case '+': + operands.push_back(a + b); + break; + case '-': + operands.push_back(a - b); + break; + case '*': + operands.push_back(a * b); + break; + case '/': + operands.push_back(a / b); + break; + } + } + } + return operands[0]; +} + +bool Solution::test(vector &tokens, int answer) +{ + return evalRPN(tokens) == answer; +} \ No newline at end of file diff --git a/150-evaluate-reverse-polish-notation/soln.hpp b/150-evaluate-reverse-polish-notation/soln.hpp new file mode 100644 index 0000000..fc48d7f --- /dev/null +++ b/150-evaluate-reverse-polish-notation/soln.hpp @@ -0,0 +1,8 @@ +#include +using namespace std; +class Solution +{ +public: + int evalRPN(vector &tokens); + bool test(vector &tokens, int answer); +}; \ No newline at end of file diff --git a/155-min-stack/driver.cpp b/155-min-stack/driver.cpp new file mode 100644 index 0000000..e7aa523 --- /dev/null +++ b/155-min-stack/driver.cpp @@ -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; +} \ No newline at end of file diff --git a/155-min-stack/soln.cpp b/155-min-stack/soln.cpp new file mode 100644 index 0000000..8f1b365 --- /dev/null +++ b/155-min-stack/soln.cpp @@ -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); +} \ No newline at end of file diff --git a/155-min-stack/soln.hpp b/155-min-stack/soln.hpp new file mode 100644 index 0000000..3fecb8c --- /dev/null +++ b/155-min-stack/soln.hpp @@ -0,0 +1,15 @@ +#include +using namespace std; +class MinStack +{ +private: + vector> _stack; + int total_min; + +public: + MinStack(); + void push(int val); + void pop(); + int top(); + int getMin(); +}; \ No newline at end of file