Skip to the workspace
Pebellslearn by building
Planning DP  ›  Capstone Project

Capstone: Meridian's Day Planner

55 minCompose the table shapes, then prove the answer: slow to find, fast to check

dsa / Capstone: Meridian's Day Planner

Explore each scene at your own pace.

01 · Recognising the shape

Recognising the shape

Capstone: Meridian's Day Planner

Definition
This capstone combines a capacity limit from knapsack with a bitmask table of shortest routes over every set of jobs, then picks the set whose profit minus driving cost is highest among sets that fit within the load limit.
In plain words
Meridian's planner is deciding which jobs to accept for one day. For every possible group of jobs it works out the shortest round trip, subtracts that from the pay, throws out groups that are too heavy, and keeps the best of what is left.

In short: this capstone chains the section together — the knapsack lesson's capacity check, the bitmask lesson's exact-tour table, and its walk-back — to score every possible set of jobs by the shortest round that visits them, and keep the best legal one. The twist is that the most profitable set on paper can be illegal (too heavy), and the best legal set can leave out the richest job, because profit depends on both what you load and how far driving it costs, and neither greedy rule alone sees both.

A row of eight set labels — {} {1} {2} {3} {1,2} {1,3} {2,3} {1,2,3} — with profits 0, 20, -8, -12, 24, 26, 10 and "over" written under {1,2,3}, the {1,3} cell circled as the winner

Every morning the planner is given the day's jobs, one van, a weight limit, and the cost of driving a kilometre. Each job sits at a stop and pays a fee, and the streets between stops are one-way. A job at a far stop might pay less than the fuel it takes to get there, and the van can't carry everything. Which jobs to take and in what order to drive them can't be decided separately. The best set depends on the round, and the round depends on the set.

This capstone puts the section together. You'll use knapsack's capacity check, the bitmask tour table and the walk-back, and then ask a question that none of the earlier lessons did: how do you know the plan you were handed is any good?

Recognising the shape

Every lesson in this section was the same idea, a table of subproblem answers filled in dependency order. The part that changed was the state. Before writing any code, name the state:

The question sounds like… State Lesson
"choose items under a limit" item × capacity (× a second limit) knapsack
"move across a map, only forwards" row × column (× direction, × resource) grids
"a hierarchy, each part depends on its children" node × a few flags trees
"which of a small set is done, and where am I" set × current item bitmask
"best of the last k" index, with a deque over the window optimisation
"a contiguous stretch" left end × right end interval DP (not covered here)

The day planner is a set problem, since there are at most eight stops and the rest of the round depends only on which are done and where the van is. It also has a limit on top, which is where the knapsack check comes in.

Scene 1 of 9
Project context and further resources
You're building a piece ofMeridian — the day planner
This piece — plan_day(): Chooses the day's jobs and their round together, and proves the plan it returns.
Scenario Each morning Meridian's planner gets the day's jobs, one van, a weight limit and the cost of a kilometre. A job at a far stop might pay less than the fuel to reach it, and the van can't carry everything, so which jobs to take and in what order have to be decided together. Before the plan goes to the driver, a checker proves it's valid in one pass.
plan_day.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
plan_day(jobs, capacity, dist, km_cost) → dictThe most profitable day: which stops to serve, in what order, the km driven and the profit.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.