📖 Scenario: Imagine you are organizing a small event and need to select activities to maximize fun within a limited time. Some activities can be chosen partially, while others must be chosen fully or not at all. You want to learn how to decide when to use a simple greedy approach or a more careful dynamic programming method.
🎯 Goal: You will create a list of activities with their durations and fun values, set a total available time, then write code to select activities using a greedy approach and a dynamic programming approach. Finally, you will print the total fun value from each method to compare results.
📋 What You'll Learn
Create a list of activities with exact durations and fun values
Set a total available time as an integer variable
Implement a greedy selection based on fun per unit time
Implement a dynamic programming solution for exact selection
Print the total fun values from both methods
💡 Why This Matters
🌍 Real World
Choosing activities or tasks to maximize benefit within limited resources is common in event planning, budgeting, and scheduling.
💼 Career
Understanding when to use greedy algorithms versus dynamic programming helps software engineers optimize solutions for resource allocation, scheduling, and decision-making problems.
Progress0 / 4 steps
1
Create the list of activities
Create a list of tuples called activities with these exact entries: (2, 10), (3, 14), (1, 7), (4, 20), (2, 8). Each tuple represents (duration, fun).
DSA C#
Hint
Use List<(int, int)> and add the exact tuples inside curly braces.
2
Set the total available time
Add an integer variable called totalTime and set it to 7.
DSA C#
Hint
Declare int totalTime = 7; inside the Main method.
3
Implement the greedy selection
Write code to sort activities by descending fun per duration ratio, then select activities greedily until totalTime is used up or no more activities fit. Store the sum of fun values in an integer variable called greedyFun.
DSA C#
Hint
Sort using OrderByDescending on fun/duration, then loop and add activities if they fit.
4
Implement dynamic programming and print results
Implement a dynamic programming solution to find the maximum fun value achievable within totalTime using the activities exactly (0/1 knapsack). Store the result in an integer variable called dpFun. Then print both greedyFun and dpFun on separate lines.
DSA C#
Hint
Use a 2D array for DP with dimensions n+1 by totalTime+1. Update dp values by choosing or skipping each activity. Print both results.
Practice
(1/5)
1. Which of the following best describes when to use a greedy algorithm instead of dynamic programming?
easy
A. When the problem has the greedy choice property and optimal substructure
B. When the problem requires checking all possible solutions
C. When the problem has overlapping subproblems but no optimal substructure
D. When the problem is about sorting large datasets
Solution
Step 1: Understand greedy choice property
A greedy algorithm works when choosing the best option at each step leads to a global optimum.
Step 2: Check optimal substructure
Optimal substructure means the optimal solution can be built from optimal solutions of subproblems.
Final Answer:
When the problem has the greedy choice property and optimal substructure -> Option A