-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProblem746.java
146 lines (114 loc) · 3.05 KB
/
Problem746.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* This problem was asked by Amazon.
* Implement a stack that has the following methods:
* - push(val), which pushes an element onto the stack
* - pop(), which pops off and returns the topmost element of the stack. If there are no elements in the stack, then it should throw an error or return null.
* - max(), which returns the maximum value in the stack currently. If there are no elements in the stack, then it should throw an error or return null.
* Each method should run in constant time.
*
*/
public class Problem746 {
public static void main(String[] args) {
StackWithMax stack = new StackWithMax();
assert stack.size() == 0;
stack.push(10);
assert stack.size() == 1;
assert stack.max() == 10;
stack.push(45);
assert stack.size() == 2;
assert stack.max() == 45;
assert stack.pop() == 45;
assert stack.size() == 1;
assert stack.max() == 10;
stack.push(456);
assert stack.size() == 2;
assert stack.max() == 456;
stack.push(1);
assert stack.size() == 3;
assert stack.max() == 456;
stack.push(42);
assert stack.size() == 4;
assert stack.max() == 456;
stack.push(657);
stack.push(555);
stack.push(765);
assert stack.max() == 765;
stack.pop();
assert stack.max() == 657;
stack.pop();
assert stack.max() == 657;
stack.pop();
assert stack.max() == 456;
assert stack.pop() == 42;
assert stack.size() == 3;
assert stack.pop() == 1;
assert stack.size() == 2;
assert stack.pop() == 456;
assert stack.size() == 1;
assert stack.pop() == 10;
assert stack.size() == 0;
}
}
class StackWithMax extends Stack {
private Stack max;
public StackWithMax() {
super();
max = new Stack();
}
@Override
public void push(int data) {
super.push(data);
if (max.size() == 0 || max.peek() < data) {
max.push(data);
}
}
@Override
public int pop() {
int data = super.pop();
if (data == max.peek()) {
max.pop();
}
return data;
}
public int max() {
assertHasItems();
return max.peek();
}
}
class Stack {
private LinkedNode root;
private int size;
public Stack() {
size = 0;
}
public void push(int data) {
LinkedNode added = new LinkedNode();
added.data = data;
added.next = root;
root = added;
size++;
}
public int pop() {
assertHasItems();
int data = root.data;
root = root.next;
size--;
return data;
}
public int peek() {
assertHasItems();
return root.data;
}
public int size() {
return size;
}
protected void assertHasItems() {
if (size <= 0) {
throw new RuntimeException();
}
}
}
class LinkedNode {
int data;
LinkedNode next;
}