Problem Solving — CS & DS

Lecture 23 — Greedy and Dynamic Programming

Learning objectives

Case-based lecture: you attempt, we discuss, then we reveal.

Today

The plan

Key definitions

Key definitions

Explain each in one breath; the cases will do the heavy lifting.
Worked example

The Meeting Room Marathon: greedy by earliest end

cs-090

Meetings (9,10.5), (9.5,11), (11,12), (10.5,12.5). Book as many non-overlapping meetings as possible in one room.

meetings = [(9, 10.5), (9.5, 11), (11, 12), (10.5, 12.5)]
ms = sorted(meetings, key=lambda m: m[1])
taken = 0
last_end = float("-inf")
chosen = []
for s, e in ms:
    if s >= last_end:
        taken += 1
        last_end = e
        chosen.append((s, e))
print(taken, chosen)

Expected: 2 meetings: (9, 10.5) and (11, 12)

Cost: O(n log n) for the sort, O(n) for the sweep.

Worked example

The Tolerance Ladder: greedy that must be checked

cs-091

A tolerance budget of 10 units; items cost 4, 5, 6, 7. Maximise how many items fit - then find a cost profile where 'cheapest first' fails.

costs = [4, 5, 6, 7]
budget = 10
costs.sort()
taken = spent = 0
for c in costs:
    if spent + c <= budget:
        taken += 1
        spent += c
print(taken, spent, budget - spent)

Expected: 2 items (4 + 5), leftover 1 - optimal for count, NOT for leftover (4+6 fills exactly)

Cost: O(n log n) sort dominates; the check-for-counterexample habit costs nothing and saves wrong answers.

Worked example

The Budgeted Syllabus: dynamic programming on weight

cs-092

Topics with (hours, points): (3,60), (2,50), (4,80), (1,30). Study budget 5 hours; maximise total points (each topic taken at most once).

topics = [(3, 60), (2, 50), (4, 80), (1, 30)]
budget = 5
dp = [0] * (budget + 1)
for h, p in topics:
    for j in range(budget, h - 1, -1):
        dp[j] = max(dp[j], dp[j - h] + p)
print(dp[budget])

Expected: 110 points (topics (2,50) and (3,60))

Cost: O(items x budget) time and O(budget) space - the classic DP price, paid to buy optimality that greedy cannot guarantee here.

Case attempt · lecture 23
Case cs-089 · Beginner

The Coin Conjecture

Greedy coin change works for one coin system and fails for another. Find the failing case, explain why, and state when greedy is safe.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-089 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-089 — not embedded in this deck.

Case attempt · lecture 23
Case cs-090 · Foundational

The Meeting Room Marathon

Choose the most non-overlapping meetings. Sort by end time; argue the choice with an exchange argument. Greedy with a proof-shaped justification.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-090 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-090 — not embedded in this deck.

Case attempt · lecture 23
Case cs-091 · Intermediate

The Tolerance Ladder

Longest increasing subsequence of exam scores. Define the DP state, transition, and read the answer; then reconstruct the sequence itself.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-091 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-091 — not embedded in this deck.

Case attempt · lecture 23
Case cs-131 · Intermediate

The Two-Desk Counter

A campus help desk serves students one at a time. Eight students will arrive today; their arrival minute and service duration (in minutes) are known in advance:

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-131 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-131 — not embedded in this deck.

Case attempt · lecture 23
Case cs-143 · Intermediate

The Shelf Replenishment

A corner shop has 20 shelves. One assistant visits each shelf **once per day, in a fixed order**, and can restock only what fits in their cart (capacity 30 units). Each shelf sells a known number of units per day:

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-143 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-143 — not embedded in this deck.

Case attempt · lecture 23
Case cs-092 · Advanced

The Budgeted Syllabus

Choose topics to study under an hour budget, maximising expected marks (weights and values). Knapsack DP; the table as the solution, not just the number.

⏱ 5 minutes — attempt now

Pseudocode, flowchart or Python. State one assumption. Compare with your neighbour at minute 4.

Discussion

cs-092 — discuss

Instructor: the reveal, the reasoning section and common wrong turns are in the instructor distribution for cs-092 — not embedded in this deck.

Check yourself

Common misconceptions

Summary

Summary & exit question

Exit question (choose one, 60 seconds)

  1. What one input kills cs-089's greedy?
  2. State the exchange argument for earliest-end scheduling.
  3. What makes a DP state adequate?
Expected answers (instructor): the non-canonical coin set instance from its instructor page swapping a later-ending chosen meeting for an earlier-ending one never hurts it captures everything the future decisions need to know
→ / space: next · ←: back · Home/End: jump · Ctrl+P → PDF