贪心¶
Introduction¶
Greedy algorithms make the locally optimal choice at each step, hoping to find the global optimum. They are widely used for optimization problems where choosing the best option at each stage leads to an overall optimal solution.
Typical Applications¶
- Interval scheduling (e.g., activity selection)
- Coin change (when denominations are canonical)
- Huffman coding
- Minimum spanning tree (Kruskal, Prim)
- Dijkstra's shortest path (with non-negative weights)
- Task assignment and resource allocation
- String and array problems (e.g., jump game, partitioning)
Classic Problems¶
- 122. Best Time to Buy and Sell Stock II
- 435. Non-overlapping Intervals
- 452. Minimum Number of Arrows to Burst Balloons
- 860. Lemonade Change
- 455. Assign Cookies
- 406. Queue Reconstruction by Height
- 135. Candy
- 605. Can Place Flowers
Greedy Solution Template¶
Key Points & Pitfalls¶
- Greedy choice must be proven to lead to global optimum (often via exchange argument or proof by contradiction)
- Not all problems with local choices are suitable for greedy; always check for counterexamples
- Sorting is often a key step in greedy solutions
- Sometimes multiple greedy strategies exist; test and prove correctness