Implement 2 stack in an array
Pattern: Implementation
Stack 1 grows from the left end and stack 2 from the right end of one array. The array is full only when the two tops meet, so neither stack wastes space the other could use.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Build TwoStacks(n) keeping two stacks in one array of size n: push1(x), push2(x) return true, or false
when there is no room; pop1() and pop2() return the top or -1 when that stack is empty.
The catalogue has no practice link; these return values are this guide's own.
Think first
- What is wrong with giving each stack half the array?
- Where could each stack start?
- When is the array really full?
Hints
Brute force, and why it fails
Two fixed halves of n / 2 each: simple, but a push can fail while half the array is free.
The key insight
Growing towards each other lets each stack use every free slot, and fullness is one comparison.
Dry run
| top1 | -1 |
|---|---|
| top2 | 4 |
TwoStacks(4): top1 = -1, top2 = 4.
The plan
push1(x): if top1 + 1 == top2: return false
top1 += 1; arr[top1] = x; return true
push2(x): if top1 + 1 == top2: return false
top2 -= 1; arr[top2] = x; return true
pop1(): if top1 == -1: return -1; return arr[top1--]
pop2(): if top2 == n: return -1; return arr[top2++]
Complexity
- Time
O(1) per operation- Space
O(n)
Each operation moves one index.
Edge cases
- n = 1: whichever stack pushes first takes the only slot.
- One stack fills the whole array.
- Popping an empty stack:
-1.
I've built my logic, show the code
Java
class TwoStacks {
private final int[] arr;
private int top1, top2; // stack 1 grows from the left, stack 2 from the right
public TwoStacks(int n) {
arr = new int[n];
top1 = -1;
top2 = n;
}
public boolean push1(int x) {
if (top1 + 1 == top2) return false; // the two tops have met: the array is full
arr[++top1] = x;
return true;
}
public boolean push2(int x) {
if (top1 + 1 == top2) return false;
arr[--top2] = x;
return true;
}
public int pop1() {
return top1 == -1 ? -1 : arr[top1--];
}
public int pop2() {
return top2 == arr.length ? -1 : arr[top2++];
}
}C++
class TwoStacks {
public:
vector<int> arr;
int top1, top2; // stack 1 grows from the left, stack 2 from the right
TwoStacks(int n) : arr(n), top1(-1), top2(n) {}
bool push1(int x) {
if (top1 + 1 == top2) return false; // the two tops have met: the array is full
arr[++top1] = x;
return true;
}
bool push2(int x) {
if (top1 + 1 == top2) return false;
arr[--top2] = x;
return true;
}
int pop1() {
return top1 == -1 ? -1 : arr[top1--];
}
int pop2() {
return top2 == static_cast<int>(arr.size()) ? -1 : arr[top2++];
}
};Python
class TwoStacks:
def __init__(self, n: int):
self.arr = [0] * n
self.top1, self.top2 = -1, n # stack 1 grows from the left, stack 2 from the right
def push1(self, x: int) -> bool:
if self.top1 + 1 == self.top2:
return False # the two tops have met: the array is full
self.top1 += 1
self.arr[self.top1] = x
return True
def push2(self, x: int) -> bool:
if self.top1 + 1 == self.top2:
return False
self.top2 -= 1
self.arr[self.top2] = x
return True
def pop1(self) -> int:
if self.top1 == -1:
return -1
self.top1 -= 1
return self.arr[self.top1 + 1]
def pop2(self) -> int:
if self.top2 == len(self.arr):
return -1
self.top2 += 1
return self.arr[self.top2 - 1]Common mistakes
- Checking only one stack's own limit, so the stacks overwrite each other.
- Starting
top2atn - 1instead ofn.