Skip to content

Commit

Permalink
impl Min Stack
Browse files Browse the repository at this point in the history
  • Loading branch information
SKTT1Ryze committed Mar 7, 2024
1 parent 923bf03 commit bf13584
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions leetcode-cc/MinStack.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#include <algorithm>
#include <climits>
#include <stack>

#include "TestHelper.h"
#include "problem.h"
#include "solution.h"
Expand All @@ -8,7 +12,7 @@ IMPLEMENT_PROBLEM_CLASS(PMinStack, 155, DIFFI_MEDIUM, TOPIC_ALGORITHMS,
"Min Stack",
"Design a stack that supports push, pop, top, and "
"retrieving the minimum element in constant time.",
{""});
{"Stack"});

class SMinStack : public ISolution {
public:
Expand All @@ -23,14 +27,24 @@ class SMinStack : public ISolution {
private:
class MinStack {
public:
MinStack() {}
MinStack() {
s = {};
s.push({0, INT_MAX});
}

void push(int val) {
int preMin = this->s.top().second;
auto element = make_pair(val, min(val, preMin));
this->s.push(element);
}

void push(int val) {}
void pop() { this->s.pop(); }

void pop() {}
int top() { return this->s.top().first; }

int top() {}
int getMin() { return this->s.top().second; }

int getMin() {}
private:
stack<pair<int, int>> s;
};
};

0 comments on commit bf13584

Please sign in to comment.