SMALLTALK LAB
Weighted interval scheduling in Squeak
Choose the most valuable set of non-overlapping bookings. Build a predecessor table, compute the best score and recover the selected jobs.
Before you begin
Each job is #(start finish value) with start strictly before finish. You have one room. A job finishing exactly when another starts does not clash. No job is mandatory, so zero is a valid total. Open a Squeak Workspace; the examples need no packages.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.
01 - See why the greedy shortcut fails
Two early bookings fit, but a single long booking is worth more. Earliest finish solves a different problem: maximizing how many jobs fit.
| early next long |
early := #(1 3 4).
next := #(3 5 4).
long := #(2 5 11).
{early second <= next first. early third + next third. long third}Expected: #(true 8 11)

Try it: The first two jobs can share the room, but 11 beats 8.
02 - Order the jobs by finish time
Sort a copy so every potential predecessor is earlier in the array. Use start time to break ties consistently on this input. The predicate uses <= for that tie-break, as Squeak sorting accepts equality.
| input jobs |
input := #(#(5 7 8) #(1 3 4) #(2 5 11)
#(3 5 4) #(0 7 15) #(7 9 3)).
jobs := input sorted: [:a :b |
a second = b second
ifTrue: [a first <= b first]
ifFalse: [a second < b second]].
jobs asArrayExpected: #(#(1 3 4) #(2 5 11) #(3 5 4) #(0 7 15) #(5 7 8) #(7 9 3))

Try it: Find the two jobs finishing at time 5. Starting earlier places #(2 5 11) first.
03 - Find the rightmost compatible predecessor
For each job, search only indices 1 through j − 1. When a finish is on time, move lo right and keep looking. The final hi gives the last compatible index, or zero. The pred array is not itself a schedule.
| jobs prev |
jobs := #(#(1 3 4) #(2 5 11) #(3 5 4)
#(0 7 15) #(5 7 8) #(7 9 3)).
prev := Array new: jobs size withAll: 0.
1 to: jobs size do: [:j |
| lo hi mid start |
start := (jobs at: j) first.
lo := 1. hi := j - 1.
[lo <= hi] whileTrue: [
mid := (lo + hi) // 2.
(jobs at: mid) second <= start
ifTrue: [lo := mid + 1]
ifFalse: [hi := mid - 1]].
prev at: j put: hi].
prevExpected: #(0 0 1 0 3 5)

Try it: Job 5 starts at 5. Jobs 2 and 3 both finish at 5, so predecessor 3 is the correct right boundary.
04 - Fill the table with take-or-skip choices
The best answer for the first j jobs either excludes job j, retaining the best for j − 1, or includes j plus the best up to its predecessor. Store the answer for zero jobs at Smalltalk index 1, so a problem size k is stored at k + 1.
| jobs prev best |
jobs := #(#(1 3 4) #(2 5 11) #(3 5 4)
#(0 7 15) #(5 7 8) #(7 9 3)).
prev := #(0 0 1 0 3 5).
best := Array new: jobs size + 1 withAll: 0.
1 to: jobs size do: [:j |
| take skip |
take := (jobs at: j) third + (best at: (prev at: j) + 1).
skip := best at: j.
best at: j + 1 put: (take max: skip)].
bestExpected: #(0 4 11 11 15 19 22)

Try it: At job 5, taking 8 plus the prefix optimum 11 beats skipping for 15. That prefix optimum picks job 2, not necessarily job 3.
05 - Trace back the winning bookings
Start at the final prefix. If its score exceeds the preceding prefix score, this job had to be taken; jump to its predecessor. Otherwise skip it, consistently preferring to skip ties. Prepend the picked job to restore chronological order.
| jobs prev best picked cursor |
jobs := #(#(1 3 4) #(2 5 11) #(3 5 4)
#(0 7 15) #(5 7 8) #(7 9 3)).
prev := #(0 0 1 0 3 5).
best := #(0 4 11 11 15 19 22).
picked := OrderedCollection new.
cursor := jobs size.
[cursor > 0] whileTrue: [
(best at: cursor + 1) > (best at: cursor)
ifTrue: [
picked addFirst: (jobs at: cursor).
cursor := prev at: cursor]
ifFalse: [cursor := cursor - 1]].
picked asArrayExpected: #(#(2 5 11) #(5 7 8) #(7 9 3))

Try it: Check adjacent endpoints: 5 touches 5 and 7 touches 7; none overlap.
06 - Use the whole solver on fresh input
Put sorting, predecessors, table and traceback into one block. It accepts unsorted input and returns the total and chosen jobs. Test the main sample, empty input and a negative job in the same run.
| solve |
solve := [:input |
| jobs prev best picked cursor |
jobs := input sorted: [:a :b |
a second = b second
ifTrue: [a first <= b first]
ifFalse: [a second < b second]].
prev := Array new: jobs size withAll: 0.
1 to: jobs size do: [:j |
| lo hi mid start |
start := (jobs at: j) first.
lo := 1. hi := j - 1.
[lo <= hi] whileTrue: [
mid := (lo + hi) // 2.
(jobs at: mid) second <= start
ifTrue: [lo := mid + 1]
ifFalse: [hi := mid - 1]].
prev at: j put: hi].
best := Array new: jobs size + 1 withAll: 0.
1 to: jobs size do: [:j |
| take skip |
take := (jobs at: j) third + (best at: (prev at: j) + 1).
skip := best at: j.
best at: j + 1 put: (take max: skip)].
picked := OrderedCollection new.
cursor := jobs size.
[cursor > 0] whileTrue: [
(best at: cursor + 1) > (best at: cursor)
ifTrue: [
picked addFirst: (jobs at: cursor).
cursor := prev at: cursor]
ifFalse: [cursor := cursor - 1]].
{best last. picked asArray}].
{solve value: #(#(5 7 8) #(1 3 4) #(2 5 11)
#(3 5 4) #(0 7 15) #(7 9 3)).
solve value: #().
solve value: #(#(1 2 -3))}Expected: #(#(22 #(#(2 5 11) #(5 7 8) #(7 9 3))) #(0 #()) #(0 #()))

Try it: Both the empty input and the negative-only input should return score 0 and no bookings.
Replay the checkpoints

Reason about correctness and limits
Sorting and all binary predecessor searches take O(n log n) time; filling and tracing the dynamic program take O(n) time. The arrays and returned schedule need O(n) space.
Insist on start < finish for this example. If touching endpoints must clash, change the compatibility test from <= to <. That is a different booking rule and can change the answer.
Do not greedily pick the latest compatible job. The predecessor is only a boundary on allowed earlier work; the best table supplies that prefix’s winning combination.
Three practice challenges
Endpoints policy
What is the optimum if jobs that touch are not allowed in the same room schedule?
Show solution
With a strict finish < start rule, #(0 7 15) alone gives 15. None of the allowed pairs beats it.
Equal scores
Try #( #(0 1 5) #(0 2 5) ). Which job survives your tie policy?
Show solution
The earlier-finishing #(0 1 5) remains selected because the second prefix does not improve the score.
Mixed profit
Try #(#(0 2 7) #(2 3 -2) #(3 4 6)). Predict which jobs are chosen.
Show solution
Pick #(0 2 7) and #(3 4 6), for 13. Adding the negative booking is allowed but hurts the score.
Reference
Princeton: Weighted interval scheduling and dynamic programming
Continue learning
Interval Scheduling | Binary Search | Knapsack
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.