Beginner's Guide to Greedy

Revision en9, by dominique38, 2026-02-10 15:04:08

This blog post is a submission for the Codeforces Month of Blog Posts Pt. III challenge. Thank you cadmiumky for the initiative!

This is how I wish I had been introduced to Greedy. Learning how to prove is the truly valuable skill, one that I believe good Greedy problems test rigorously.

Introduction

After struggling with proofs for quite a while, there are a few things I realized. Greedy proofs are very dependent on the rules of the problem. Loosely speaking each optimization problem gives you observations, from those observations, you realize that if you take certain choices while avoiding all others, then it will always be optimal. That realization is termed Greedy.

There are a lot of optimization problems, but only a small subset allows you to break the problem into smaller independent subproblems where solving them optimally leads to the best global answer. This property, known as Optimal Substructure, is found in both DP and Greedy algorithms. We won't go into those details here, instead we will generally assume our problems have the Optimal Substructure property.

Short Note

While DP and Greedy share the optimal substructure property, DP is essentially "smart brute force", where you define the subproblems and check all transitions to find the best one. In Greedy, however, you have to rigorously prove that ignoring all other choices and taking just one specific path works every single time. This takes the difficulty one notch up from DP.

The idea behind DP is the same everywhere, gather enough observations to define your subproblems, transitions, and base cases. This same idea is explored in a lot of diverse places—there is DP on Bitmasks, Trees, Graphs, Digits, Ranges, Weird DP Sorcery? and On and on.., but there are not many discussions on Greedy. To fill this gap, I would like to discuss a lot of problems and proofs to capture the essence of how to prove Greedy solutions.

Idea

To prove a greedy solution, the arguments are almost always Proof by Contradiction or Induction. The argument goes like this: let's assume a greedy solution $$$G$$$ and a magical optimal solution $$$O$$$ that doesn't follow the greedy choices. Then, given our observations, we prove that either $$$G$$$ performs no worse than $$$O$$$, or that given the constraints, the final solution produced by both strategies will look exactly the same. On the flip side, among many greedy choices, to disprove one, the easiest way is to find a counter-example.

Below, I have listed links to various problems and discussed the greedy ideas they use, along with some common greedy examples. Sometimes Greedy seems obvious and intuitive, but I encourage you to try forming a rigorous argument while proving the problems mentioned below.

Problems

I. Basic problems (without links)

Try to come up with formal arguments to get the hang of it. Basic doesn't always mean easy.

P1. I have $$$n$$$ items and a knapsack of capacity $$$W$$$. The $$$i$$$-th item has value $$$v_i$$$ and weight $$$w_i$$$. I am allowed to take fractions of items. Find the strategy that maximizes the total value.

Hint
Solution

P2.I am running along an infinitely long number line. I can run for at most $$$M$$$ units without refreshments. There are $$$n$$$ refreshment stalls, where the $$$i$$$-th stall is located at $$$x_i$$$. Find the strategy that minimizes the number of refreshment breaks.

Hint
Solution

P3. Given two integer arrays $$$a$$$ and $$$b$$$ of size $$$n$$$ filled with positive integers, reorder the elements within the arrays to maximize $$$\prod_{i=1}^{n} a_{i}^{b_i}$$$. Too easy :P

Hint
Solution

P4. Given an array of $$$n$$$ activities, where the $$$i$$$-th activity takes $$$p_i$$$ time to complete. I want to schedule activities such that they don't overlap.

  • Let $$$c_i$$$ be the completion time of the $$$i$$$-th activity in the schedule. If the $$$i$$$-th activity starts at time $$$x$$$, then its completion time is $$$c_i = x + p_i$$$. Goal is to minimize $$$\sum_{i=1}^{n} c_i$$$ (which is equivalent to minimizing the average completion time).
Hint
Solution
  • Each activity has a start time $$$s_i$$$ and a finish time $$$f_i$$$. Find the maximum number of non-overlapping activities you can perform.
Hint
Hint
Solution

Note: The graph is denoted by $$$H$$$, as the letter $$$G$$$ represents the Greedy solution.

By now, we must have gotten some idea about how to find properties that connect the Greedy solution $$$G$$$ and the Optimal solution $$$O$$$, eventually proving they are the same.The same logic applies to problems involving graphs or trees. We often assume the existence of a specific optimal tree $$$T_{O}$$$, a subgraph $$$H_{O} \subset H$$$, or even a partially constructed subtree $$$T_{O}' \subset T_{O} \subset H$$$ , basically anything concrete that we can "play with" to formulate rigorous arguments about their behavior.

P5. Given an undirected graph $$$H=(V,E)$$$ with non-negative weights.

  • Find its Minimum Spanning Tree (MST).
Hint
Hint
Solution
  • Find a strategy to find the Second Best Minimum Spanning Tree.
Hint
Solution
Bonus
II. Basic Problems (with Links)
Random Thought
  1. Kanade's Perfect Multiples,
  2. Game on Array,
  3. Stay or Mirror,
  4. Ticket Hoarding,
  5. Maximum Running time of N computers,
  6. Group Increases,
  7. Xor factorization,
  8. Divine Tree,
  9. Tracks in the Snow
  10. Find the k-sum of an array
  11. Erasing Vertices 2
  12. Path and Subsequence

Outro

I hope this blog helps both beginners and intermediate problem solvers.

These are just a few of the interesting Greedy problems I've shared. If you know of other problems with elegant or tricky Greedy arguments, please share them! Also, if you spot any mistakes or if any part of the explanation wasn't clear, don't hesitate to point it out.

Happy Solving! ^_^

Tags greedy, mathematical induction

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en11 English dominique38 2026-02-12 19:00:11 183 Final Draft 2: Clarified the wording in P2.
en10 English dominique38 2026-02-10 15:19:29 2 (published)
en9 English dominique38 2026-02-10 15:04:08 23902 Final Draft 1
en8 English dominique38 2026-01-31 21:58:10 3923 Completed P1 to P5. Added little language correction.
en7 English dominique38 2026-01-31 17:18:09 5005 All P1 to P5 done. Remaining are the problem links.
en6 English dominique38 2026-01-29 14:50:47 2 Minor typo change
en5 English dominique38 2026-01-29 14:49:28 0 No change: Added Coauthors.
en4 English dominique38 2026-01-29 14:34:57 0 No change: Added Coauthors.
en3 English dominique38 2026-01-29 14:32:37 6118 Addition: P5 half done. P4 remaining. CF problems remaining.
en2 English dominique38 2026-01-28 22:25:05 8360 Addition: P1, P2 and P3 are done. P4 and P5 and onward are remaining.
en1 English dominique38 2026-01-28 17:16:48 4669 Initial revision: Added the basic structure, and rough content. (saved to drafts)