Greedy Subgoal Scheduling Under a Step Budget
Problem Statement
An agent must pick which subgoals to pursue within a fixed step budget to maximize total value. Each subgoal costs some steps and yields some value; pick the subset with the greatest value that fits.
Background
This is 0/1 knapsack: with budget steps and subgoals each {"steps": int, "value": int}, choose a subset whose total steps <= budget maximizing total value. Return the max achievable value.
Your Task
def schedule_subgoals(subgoals, budget):
Return the maximum total value (int).
Input Format
- subgoals (list of dicts), budget (int).
Output Format
- A single int.
Sample
print(schedule_subgoals([{"steps":2,"value":3},{"steps":3,"value":4},{"steps":4,"value":5}], 5))
Output:
7
Example
print(schedule_subgoals([{"steps":2,"value":3},{"steps":3,"value":4},{"steps":4,"value":5}], 5))7
- Initialize a value tracker for step budgets 0 through 5, starting with all zeros: .
- Process the first subgoal (cost 2, value 3): update the tracker so that any budget can hold this item. The tracker becomes , meaning a budget of 2 or more yields a value of 3.
- Process the second subgoal (cost 3, value 4): check if adding this item improves the value for budgets . For budget 5, combining the first subgoal (cost 2, value 3) with this one (cost 3, value 4) exceeds the budget, but for budget 5, we compare keeping the previous best (3) versus taking this item alone (4) or combining with a previous item that fits. Specifically, at budget 5, , which is better than the current 3. The tracker updates to .
- Process the third subgoal (cost 4, value 5): check budgets . For budget 5, combining this with the first subgoal (cost 2, value 3) gives total cost 6, which exceeds the budget. Taking it alone gives value 5, which is less than the current best of 7. The tracker remains .
- The final output is 7
Constraints
0 <= budget <= 5000, non-negative int steps/values,len <= 200.- Classic 0/1 knapsack; each subgoal used at most once.
- Return the max value.