A greedy algorithm commits to one choice at a time without revisiting earlier decisions. The hard part is not writing the loop; it is proving that each commitment preserves an optimal solution.
What Is It?
A greedy algorithm uses a fixed rule to make one irreversible choice at a time. The key question is: does the locally best choice stay compatible with a globally optimal answer? "Take the biggest coin" is a local choice. "Use the fewest coins total" is the global objective. Repeating the local choice is valid only when you can show that it never excludes every optimal solution.