From 4bc607f5a06839ca86de97337ab1bb476c80dcdb Mon Sep 17 00:00:00 2001 From: Vergara711 <44036219+Vergara711@users.noreply.github.com> Date: Wed, 7 Aug 2019 22:03:40 +0300 Subject: [PATCH] Create Queue with minimum --- Queue with minimum | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Queue with minimum diff --git a/Queue with minimum b/Queue with minimum new file mode 100644 index 0000000..c4cd82f --- /dev/null +++ b/Queue with minimum @@ -0,0 +1,33 @@ +class queueWithMinimum{ +private: + vector > s[2]; + static const int inf = 1e9 + 7; + +private: + int getMin(int k){ + return s[k].size() == 0 ? inf : s[k].back().second; + } + + void push(int x, int k){ + s[k].push_back({x, min(getMin(k), x)}); + } + +public: + void push(int x){ + push(x, 0); + } + + int getMin(){ + return min(getMin(0), getMin(1)); + } + + void pop(){ + if (s[1].size() == 0){ + while (s[0].size() > 0){ + push(s[0].back().first, 1); + s[0].pop_back(); + } + } + s[1].pop_back(); + } +};