-
Notifications
You must be signed in to change notification settings - Fork 2
/
SetOfStacks.java
78 lines (62 loc) · 1.65 KB
/
SetOfStacks.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
/*
Stack of Plates: Imagine a (literal) stack of plates. If the stack gets too high, it might topple.
Therefore, in real life, we would likely start a new stack when the previous stack exceeds some
threshold. Implement a data structure SetOfStacks that mimics this. SetO-fStacks should be
composed of several stacks and should create a new stack once the previous one exceeds capacity.
SetOfStacks. push() and SetOfStacks. pop() should behave identically to a single stack
(that is, pop () should return the same values as it would if there were just a single stack).
*/
class Stack {
int[] arr;
int top;
Stack(int n) {
this.arr = new int[n];
this.top = -1;
}
boolean isEmpty() {
return top == -1 ? true : false;
}
void push(int x) {
arr[++top] = x;
}
int pop(int stackCount) {
if (top == -1 && stackCount == 0) {
throw new EmptyStackException();
}
return arr[top--];
}
}
class SetOfStacks {
int capacity;
int filledCapacity;
Stack stack;
List<Stack> stacks;
SetOfStacks(int capacity) {
this.capacity = capacity;
this.filledCapacity = 0;
this.stack = new Stack(this.capacity);
this.stacks = new ArrayList<>();
}
boolean push(int x) {
if (capacity == filledCapacity) {
stacks.add(stack);
filledCapacity = 0;
stack = new Stack(capacity);
}
stack.push(x);
filledCapacity++;
return true;
}
int pop() {
if (stack.isEmpty() && stacks.size() != 0) {
stack = stacks.get(stacks.size() - 1);
}
int a = stack.pop(stacks.size());
filledCapacity--;
if (stack.isEmpty()) {
stacks.remove(stack);
filledCapacity = 0;
}
return a;
}
}