-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay38.cpp
More file actions
58 lines (46 loc) · 1.12 KB
/
Day38.cpp
File metadata and controls
58 lines (46 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
This problem was asked by Amazon.
Implement a stack API using only a heap. A stack implements the following methods:
push(item), which adds an element to the stack
pop(), which removes and returns the most recently added element (or throws an error if there is nothing on the stack)
Recall that a heap has the following operations:
push(item), which adds a new key to the heap
pop(), which removes and returns the max value of the heap
*/
#include <bits/stdc++.h>
using namespace std;
class StackAPI
{
priority_queue<pair<int, int>> pq;
int c;
public:
StackAPI() : c(0) {}
void push(int item)
{
pq.push({c++, item});
}
int pop()
{
if (pq.empty())
{
throw out_of_range("Stack is empty");
}
int p = pq.top().second;
pq.pop();
return p;
}
};
int main()
{
StackAPI s;
s.push(10);
s.push(20);
s.push(30);
cout << s.pop() << "\n"; // 30
cout << s.pop() << "\n"; // 20
s.push(40);
cout << s.pop() << "\n"; // 40
cout << s.pop() << "\n"; // 10
cout << s.pop() << "\n"; // error
return 0;
}