Greedy Algorithms
Greedy Algorithms and the Greedy Choice
Greedy algorithms are the shortest solutions and the easiest to get wrong. The code is often a sort followed by a single loop; the difficulty is entirely in justifying that taking the locally best option gives a globally optimal answer, which for many problems it does not.
Each lesson pairs the algorithm with the argument for why its greedy choice is safe. Interval problems get particular attention because they are the most common greedy family in interviews, and because the correct sort key — by end time, not start time — is exactly the sort of decision a visualizer makes obvious and a paragraph does not.
Interactive lessons
Free to open
Languages (JS, TS, Python, Java, C, C++)
Greedy Algorithms lessons
Greedy Algorithms time and space complexity compared
Best, average and worst-case running time and extra space for every greedy algorithms lesson, as each lesson derives them.
| Lesson | Best | Average | Worst | Space |
|---|---|---|---|---|
| Introduction | O(N log N) (Usually dominated by sorting) | O(N log N) | O(N log N) | O(1) (Often auxiliary space is minimal) |
| Activity Selection | O(N log N) (Sorting) | O(N log N) | O(N log N) | O(1) (Auxiliary, excluding sort stack) |
| Huffman Coding | O(N log K) (N=len, K=unique chars) | O(N log K) | O(N log K) | O(K) (Tree storage) |
| Interval Scheduling | O(N log N) (Sorting) | O(N log N) | O(N log N) | O(1) (Auxiliary) |
| Job Sequencing | Ω(N log N) | Θ(N log N + N*D) | O(N log N + N*D) | O(D) |
| Minimum Platforms | O(N log N) (Sorting) | O(N log N) | O(N log N) | O(1) (Auxiliary space for pointers) |
Continue the DSA curriculum
Greedy Algorithms is one of 12 topics in the interactive DSA visualizer curriculum. Once this topic is comfortable, the coding interview pattern library reorganises the same material around the patterns interview questions are built on.
Every other DSA visualizer
Data Structures · 28 lessons
- StackFree
- QueueFree
- Deque
- Circular Queue
- Priority Queue (ADT)
- Tree Traversal
- Binary Tree
- Binary Search Tree
- AVL Tree
- Trie (Prefix Tree)
- Segment Tree
- Fenwick Tree
- Red-Black Tree
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- Graph RepresentationFree
- Breadth First SearchFree
- Depth First Search
- Topological Sort
- Cycle Detection
- Connected Components
- Bipartite Graph
- Dijkstra's Algorithm
- Bellman-Ford Algorithm
- Minimum Spanning Tree
- Floyd-Warshall Algorithm
- Bridges & Articulation Points
