Greedy
⏱ Time: O(N log N)💾 Space: O(1)
Make the locally optimal choice at each stage with the hope of finding a global optimum. Often requires sorting the input first.
🎯 When to Use
- ✓Optimization problems
- ✓Interval merging/scheduling
- ✓Local optimum leads to global optimum
📝 Pseudocode
Greedy TemplateO(N log N)
1function solve(intervals): 2 intervals.sort() 3 res = [] 4 for interval in intervals: 5 if condition(interval): 6 res.push(interval) 7 return res