Rare
 0/11

Stacks

Authors: Darren Yao, Michael Cao

A data structures that only allows insertion and deletion at one end.

Resources
CPHbrief description of operations
CSABracket Matching Application

Stacks

A stack is a Last In First Out (LIFO) data structure that supports three operations, all in O(1)\mathcal{O}(1) time. Think of it like a real-world stack of papers.

C++

C++

  • push: adds an element to the top of the stack
  • pop: removes an element from the top of the stack
  • top: retrieves the element at the top without removing it
stack<int> s;
s.push(1); // [1]
s.push(13); // [1, 13]
s.push(7); // [1, 13, 7]
cout << s.top() << endl; // 7
s.pop(); // [1, 13]
cout << s.size() << endl; // 2

Java

Java

  • push: adds an element to the top of the stack
  • pop: removes an element from the top of the stack
  • peek: retrieves the element at the top without removing it
Stack<Integer> s = new Stack<Integer>();
s.push(1); // [1]
s.push(13); // [1, 13]
s.push(7); // [1, 13, 7]
System.out.println(s.peek()); // 7
s.pop(); // [1, 13]
System.out.println(s.size()); // 2

Application: Nearest Smaller Element

Focus Problem – read through this problem before continuing!

Consider the following problem:

Given an array, aa, of NN (1N1051 \le N \le 10^5) integers, for every index ii, find the rightmost index jj such that j<ij < i and ai>aja_i > a_j.

Resources
CPH
CSAsimilar application w/ animation

To solve this, let's store a stack of pairs of value,indexvalue, index and iterate over the array from left to right. For some index ii, we will compute ans[i]\texttt{ans}[i], the rightmost index for ii, as follows:

  • Keep popping the top element off the stack as long as valueaivalue \ge a_i. This is because we know that the pair containing valuevalue will never be the solution to any index j>ij > i, since aia_i is less than or equal to than valuevalue and has an index further to the right.
  • If value<aivalue < a_i, set ans[i]\texttt{ans}[i] to indexindex, because a stack stores the most recently added values first (or in this case, the rightmost ones), indexindex will contain the rightmost value which is less than aia_i. Then, add (ai,ia_i, i) to the stack.

The stack we used is called a monotonic stack because we keep popping off the top element of the stack which maintains it's monotonicity (the same property needed for algorithms like binary search) because the elements in the stack are increasing.

Implementation

C++

#include <bits/stdc++.h>
using namespace std;
int N;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);

Java

/**
* author: Kai Wang
*/
import java.io.*; import java.util.*;
public class NearestSmallestVals {
/**
* We keep a stack of pairs (ind, value)
* Traverse the array from left to right, use ans to store answers

Problems

StatusSourceProblem NameDifficultyTagsSolutionURL
LCNormal
Show Tags

Stack

GoldNormal
Show Tags

Stack

External Sol
GoldNormal
Show Tags

Stack

External Sol
CSESNormal
Show Tags

Stack

CEOINormal
Show Tags

Stack

Check CF
CSESHard
Show Tags

PURS, Stack

IOIVery Hard
Show Tags

Stack

InfoArenaVery Hard
Show Tags

Stack

View Solution
CSESInsane
Show Tags

Stack

View Solution
Croatian OIInsane
Show Tags

Stack

External Sol

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!