Greedy Algorithms
3 programs — patterns, complexity, and code
| # | Program Name | Pattern | Time | Space | Key Idea | Level |
|---|---|---|---|---|---|---|
| 1 | bestTimeToBuyStock | Greedy (track min) | O(n) | O(1) | Track the minimum price seen; profit = current price minus that minimum. | Simple |
| 2 | canJump | Greedy (max reach) | O(n) | O(1) | Track the furthest index reachable; if current index exceeds it, return false. | Middle |
| 3 | jumpGameII | Greedy (BFS levels) | O(n) | O(1) | Treat each jump range as a BFS level; expand the frontier greedily. | Middle |