Back to Topics
Greedy Algorithms
Make the locally optimal choice at each stage with the hope of finding a global optimum.
1. Basic Greedy (Sorting + Local Choice)
Practice Scenarios
Pseudocode Implementation
sort(arr)
for item in arr:
if valid choice:
take it
2. Interval Scheduling / Merging
Practice Scenarios
Pseudocode Implementation
sort by start time
for interval in intervals:
if overlap:
merge/update
else:
add new interval
3. Jump / Reachability Greedy
Practice Scenarios
Pseudocode Implementation
farthest = 0
for i:
if i > farthest: return false
farthest = max(farthest, i + nums[i])
4. Greedy with Heap / Priority Queue
Practice Scenarios
Pseudocode Implementation
for item:
push into heap
if constraint violated:
pop worst element
5. Greedy + Sorting + Math Insight
Practice Scenarios
Pseudocode Implementation
tank = 0
start = 0
for i:
tank += gain[i]
if tank < 0:
start = i+1
tank = 0
6. Hard Greedy / Advanced Patterns
Practice Scenarios
Pseudocode Implementation
sort by key
for item:
add to heap
maintain constraint
update answer
7. String Greedy
Practice Scenarios
Pseudocode Implementation
for char:
while stack and better choice:
pop
push char
8. Greedy + Binary Search Hybrid
Practice Scenarios
Pseudocode Implementation
low, high
while low < high:
mid = (low+high)//2
if feasible(mid):
high = mid
else:
low = mid+1