Browse curriculum

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.

6

Interactive lessons

1

Free to open

6

Languages (JS, TS, Python, Java, C, C++)

Lessons

Greedy Algorithms lessons

Complexity

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.

LessonBestAverageWorstSpace
IntroductionO(N log N) (Usually dominated by sorting)O(N log N)O(N log N)O(1) (Often auxiliary space is minimal)
Activity SelectionO(N log N) (Sorting)O(N log N)O(N log N)O(1) (Auxiliary, excluding sort stack)
Huffman CodingO(N log K) (N=len, K=unique chars)O(N log K)O(N log K)O(K) (Tree storage)
Interval SchedulingO(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 PlatformsO(N log N) (Sorting)O(N log N)O(N log N)O(1) (Auxiliary space for pointers)
Curriculum

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.

Directory

Every other DSA visualizer