DP vs Recursion vs Greedy: Choosing the Right Tool
📖 Scenario: Imagine you are organizing a small event and need to decide how to allocate your budget to buy snacks. You have a list of snack options with their costs and tastiness scores. You want to maximize tastiness without exceeding your budget.
🎯 Goal: You will create a program that helps decide which snacks to buy using three different approaches: recursion, dynamic programming, and greedy. This will help you understand when each method works best.
📋 What You'll Learn
Create a list of snack costs and tastiness scores
Set a budget limit
Implement a recursive function to find the best tastiness within budget
Implement a dynamic programming solution for the same problem
Implement a greedy approach based on tastiness-to-cost ratio
Print the maximum tastiness found by each method
💡 Why This Matters
🌍 Real World
Budgeting and resource allocation problems where you want to maximize value under constraints.
💼 Career
Understanding these approaches helps in software roles involving optimization, planning, and algorithm design.
Progress0 / 4 steps
1
Create snack data and budget
Create a vector of integers called costs with values {2, 3, 4, 5} and a vector of integers called tastiness with values {3, 4, 5, 6}. Also create an integer variable called budget and set it to 8.
DSA C++
Hint
Use vector<int> to store costs and tastiness. Set budget as an int.
2
Add recursive function to find max tastiness
Add a recursive function called maxTastinessRecursive that takes three parameters: int index, int remainingBudget, and returns an int. It should return 0 if index is equal to the size of costs. Otherwise, it should return the maximum tastiness by either skipping the current snack or buying it if the cost fits in remainingBudget.
DSA C++
Hint
Use recursion to explore both choices: skip or take the snack if budget allows.
3
Add dynamic programming solution
Add a function called maxTastinessDP that uses a 2D vector dp of size (costs.size() + 1) x (budget + 1) to store maximum tastiness values. Fill dp bottom-up and return the maximum tastiness for all snacks and full budget.
DSA C++
Hint
Use a 2D vector to store results for each snack index and budget. Fill it from the bottom up.
4
Add greedy approach and print results
Add a function called maxTastinessGreedy that creates a vector of pairs of double ratio (tastiness/cost) and index. Sort it in descending order by ratio. Then pick snacks in this order while budget allows, summing tastiness. Finally, print the results of maxTastinessRecursive(0, budget), maxTastinessDP(), and maxTastinessGreedy() each on its own line.
DSA C++
Hint
Sort snacks by tastiness-to-cost ratio and pick as many as budget allows. Then print results of all three methods.
Practice
(1/5)
1. Which approach is best suited when a problem has overlapping subproblems and optimal substructure?
easy
A. Dynamic Programming
B. Greedy Algorithm
C. Simple Recursion without memoization
D. Brute Force
Solution
Step 1: Understand problem characteristics
Overlapping subproblems means the same smaller problems repeat. Optimal substructure means the best solution can be built from best solutions of smaller problems.
Step 2: Match approach to characteristics
Dynamic Programming stores results of subproblems to avoid recomputation, ideal for overlapping subproblems and optimal substructure.