PIXELBANKv9.1.0
Menu

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:

Input:
print(schedule_subgoals([{"steps":2,"value":3},{"steps":3,"value":4},{"steps":4,"value":5}], 5))
Output:
7
Reasoning:
  • Initialize a value tracker for step budgets 0 through 5, starting with all zeros: [0,0,0,0,0,0][0, 0, 0, 0, 0, 0].
  • Process the first subgoal (cost 2, value 3): update the tracker so that any budget ≥2\ge 2 can hold this item. The tracker becomes [0,0,3,3,3,3][0, 0, 3, 3, 3, 3], 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 ≥3\ge 3. 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, dp[5−3]+4=dp[2]+4=3+4=7dp[5-3] + 4 = dp[2] + 4 = 3 + 4 = 7, which is better than the current 3. The tracker updates to [0,0,3,4,4,7][0, 0, 3, 4, 4, 7].
  • Process the third subgoal (cost 4, value 5): check budgets ≥4\ge 4. 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 [0,0,3,4,4,7][0, 0, 3, 4, 4, 7].
  • 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.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.