Comparison Mode

Side-by-side: use case, requirements, complexity, strengths, weaknesses, example problems, and a clear “choose this when…”.

GreedyGreedy
Use caseOptimization where a locally best choice can be proved safe (exchange argument).Optimization or counting where choices interact and subproblems repeat.
RequirementsGreedy-choice property and optimal substructure; usually a sort.Optimal substructure and overlapping subproblems; a well-defined state.
Time complexityTypically O(n log n) for the sort, O(n) afterwards.Number of states times work per transition, e.g. O(n * W) for knapsack.
Space complexityO(1) to O(n).One entry per state, often reducible to a rolling row.
StrengthsFast, short code, low memory.Always correct when the recurrence is correct; handles "number of ways" and multi-constraint objectives.
WeaknessesOften wrong: without a proof it silently returns suboptimal answers (coin change with coins 1, 3, 4).Slower and more memory than greedy; state design is the hard part.
Example problemsNon-overlapping intervals, jump game, gas station, fractional knapsack, Huffman coding.Coin change, 0/1 knapsack, longest increasing subsequence, edit distance.
Choose this whenChoose greedy when you can sketch an exchange argument showing the local choice never hurts; if you cannot, do not trust it.Choose DP when a greedy counterexample exists or the problem asks for counts, or when items cannot be split and choices interact.