##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
Explore individual topics with detailed lessons and practice problems.
What makes an algorithm greedy? Learn the greedy choice property and when greedy solutions work.
How do you know greedy works? Learn two proof techniques: the exchange argument and the stays-ahead argument to verify greedy strategies.
What makes an algorithm greedy? Learn the greedy choice property and when greedy solutions work.
Why greedy matters
Local choices, global optimum
The first property
Subproblems stay optimal
Test your understanding
Recognizing the pattern
The warning signs
Why greedy is faster
The key difference
Know the difference
Common structure
Standard denominations
The canonical property
Non-standard denominations
Know your input
Greedy or DP?
Entry-level greedy
Match smallest to smallest
Two-pointer approach
Tracing the algorithm
The code
Proof sketch
summary
Making change greedily
Prefer giving $10 bills
Track your bills
The code
Why prefer $10?
Resource flexibility
What to sort by?
Recognizing problem types
How to think
Apply the thinking
Usually fast
Looking ahead
What we learned
How do you know greedy works? Learn two proof techniques: the exchange argument and the stays-ahead argument to verify greedy strategies.
Why proofs matter
When feelings fail
Your toolkit
Swap to improve
The template
Test your understanding
Proving earliest-end-first
Constructing the swap
Induction finishes it
Problem characteristics
Always leading
The template
Test your understanding
Alternative proof
First activity
Maintaining the lead
Finishing the proof
Problem characteristics
Choosing your approach
Apply your knowledge
When greedy fails
Greedy ratio fails
A clearer failure
When fractional thinking fails
The blocking problem
Systematic approach
Finding failures
Proving shortest-first
Shortest first
Swapping adjacent pairs
Simple implementation
Maximize or minimize speed
Pair fast with slow
Pair fast with fast
Exchange argument
Taller in back row
Match by rank
Why sorting works
Apply the algorithm
Avoid these errors
Building proof skills
Looking ahead
What we learned
The classic greedy problem. Select the maximum number of non-overlapping activities by deadline.
The gateway problem
The classic setup
Finding the answer
Why it fails
Why it fails
Why it fails
Earliest end time
Test your understanding
Step by step
Tracing the algorithm
The code
Why it is optimal
The greedy choice
Extending the argument
G is optimal
Test your understanding
Key patterns
Same problem, different story
Minimum removals
Tracing the algorithm
The code
Recognize equivalence
Burst balloons
Grouping overlapping balloons
Sort and shoot
Tracing the algorithm
The code
Same pattern, different questions
Empty input and ties
Touching intervals
When greedy fails
A counterexample
Can attend all?
Simple overlap check
Minimum rooms needed
Event-based counting
Tracing the events
Min-heap method
The code
Apply the algorithm
Different questions
Looking ahead
What we learned
Intervals appear constantly in interviews. Merge overlapping intervals, insert intervals, and cover intervals with minimum points.
The interval pattern
Sort first, scan second
Combine overlapping
Sort by start
Step by step
Tracing the algorithm
The code
Test your understanding
Add without overlap
Three phases
The code
Key patterns
Two sorted lists
Two-pointer approach
The code
Apply the algorithm
Eliminate redundancy
Sort and track
The code
Cover a range
Greedy extension
The code
Apply the pattern
The selection key
When to use which
Minimum removals
Maximize to minimize
The code
Can attend all?
Overlap detection
The code
Minimum rooms needed
Start and end events
The code
Event sweep understanding
Processing events
Online interval insertion
Linear vs balanced
The code
The formula
Testing the formula
Know your problem type
Looking ahead
What we learned
Apply greedy thinking to arrays. Solve jump games, gas station, and other array-based problems.
Greedy on arrays
Can you reach the end?
Track furthest reachable
Tracing the algorithm
The code
Apply the algorithm
Minimum jumps
BFS-like levels
The code
Circular route
Find the valley
The code
Apply the insight
Pair heavy with light
Two-pointer pairing
The code
Distribute fairly
Two-pass greedy
The code
When to use it
Kadane's algorithm
Reset when negative
The code
The greedy insight
Understanding the reset
One transaction
Track the minimum
The code
Unlimited transactions
Collect all gains
The code
Comparing strategies
Maximum partitions
Extend to last occurrence
The code
People in line
Tallest first
The code
Why tallest first
Looking ahead
What we learned
Classic optimization problems where greedy shines: fractional knapsack, job scheduling, and task scheduling. Learn when greedy gives optimal answers.
Optimization problems
Take fractions
Sort by ratio
The code
Discrete items block
Fractional vs 0/1
Maximize profit with deadlines
Latest available slot
The code
Railway station
The code
CPU with cooldown
Count and formula
The code
Apply the formula
The idle slots insight
No adjacent duplicates
When is it possible
Interleave by frequency
The code
Feasibility check
Maximize capital
enable then maximize
The code
Wage ratios
The ratio determines cost
The code
Why ratio order
Minimum swaps
Fix each pair
The code
Common structure
Satisfy children
Match smallest to smallest
The code
Why this order
Looking ahead
What we learned
Huffman coding for compression, string rearrangement, and other advanced greedy applications.
Beyond basic greedy
Optimal prefix codes
Build the tree
The code
No adjacent duplicates
Max-heap approach
The code
Feasibility check
Minimize the number
Stack-based greedy
The code
Local removal helps globally
Understanding the stack
Merge two arrays
Three greedy steps
Monotonic stack selection
Lexicographic comparison
Putting it together
Cover all sums
Extend the range
The code
Why add miss
Eliminate matches
Mark, crush, drop
The code
Burst all balloons
Activity selection variant
The code
Spotting the connection
No duplicates
Sort and fix
The code
Common themes
Pitfalls to watch
Choosing the right approach
Practice section
What we learned
Reinforce your greedy skills with curated problems. Mix of easy, medium, and hard challenges covering all the patterns you have learned.
Practice makes permanent
Building intuition
Flip negatives
Flip smallest first
The code
Handling odd flips
Huffman-like merging
Always merge smallest
The code
Split evenly
Sort by savings
The code
Play optimally
Small face-up, large face-down
The code
Why track max score
Work backwards
Halve or add
The code
No duplicate frequencies
Reduce duplicates
The code
With wildcards
Track min and max
The code
Alternating differences
Count direction changes
The code
Why this greedy works
Pattern recognition
Build confidence
Core practice
Challenge yourself
Continue learning
What we learned