Your concept feed

29 ideas

A few small steps. No rush.

Follow the connections in this topic

Let’s picture it

We’ve answered this before.

route A
3 stairs left
route B

Two routes leave us with three stairs left. We may climb one or two at a time.

Swipe when you’re ready
One little change

Save these three little routes.

1+1+1
1+2
2+1

The routes are 1+1+1, 1+2, and 2+1. Count them once; both arrivals can reuse the answer.

Same idea. One more small step.
Now it has a name

That sharing is the heart of DP.

same question
reuse answer

Dynamic programming saves answers to smaller questions. Share only when the remaining question really is the same.

Want to try a tiny question?
No pressure. Just try it.

One route has a key left and the other doesn’t. Always the same remaining question?

You can also keep scrolling
1 / 4
Thinking in DP

One small step at a time.

This idea builds on these smaller ones. Pick any to revisit it here.

Same door. One small difference.

You + key
Locked door

You have a key. Your friend doesn’t. The door is the same, but your next move isn’t.

1 / 3
Let’s picture it

Two ways to fill a notebook.

asked?
solve once
save

One way: when a question appears, solve it if needed, then write down the answer.

Swipe when you’re ready
One little change

Or prepare the small answers first.

small
next
bigger

Fill answers in an order where each needed earlier answer is already available.

Same idea. One more small step.
Now it has a name

Same idea, different order.

on demand
in order

Saving answers on demand is memoization. Filling them in a planned order is tabulation.

Want to try a tiny question?
No pressure. Just try it.

When filling a table, should an answer be ready before another cell uses it?

You can also keep scrolling
1 / 4
Thinking in DP

One small step at a time.

This idea builds on these smaller ones. Pick any to revisit it here.

We’ve answered this before.

route A
3 stairs left
route B

Two routes leave us with three stairs left. We may climb one or two at a time.

1 / 3
A familiar idea

The dependency order is only a start.

A: 2
B: 5
C: waits

A takes 2 hours; B takes 5. Both can start now. C takes 3 and needs both finished.

Swipe when you’re ready
One new wrinkle

C waits for the slower branch.

max(2, 5)
+ 3
= 8

A and B run together. C starts at hour 5, not hour 7. It finishes at 8.

Same idea. One more small step.
Try a tiny example

Store an earliest finish per task.

prerequisite maximum
own duration
finish

finish[C] is its own duration plus the maximum finish among its prerequisites. Tasks without prerequisites start at zero.

Same idea. One more small step.
Notice what matters

Use the graph to order the work.

all parents ready
finalize finish

Kahn’s queue releases a task after all prerequisites are processed. By then, its longest incoming chain has been considered.

Same idea. One more small step.
Make it reusable

Topological sort meets DP.

order
recurrence
answer

The order tells us when. The recurrence tells us what to compute. Together they solve Parallel Courses III.

Want to try a tiny question?
No pressure. Just try it.

If only one course can run at a time, is max(2, 5) still the waiting time?

You can also keep scrolling
1 / 6
Thinking in DP

One small step at a time.

This idea builds on these smaller ones. Pick any to revisit it here.

Socks before shoes.

Socks
Shoes

Some jobs must wait for other jobs. Draw an arrow from the earlier job to the later one.

1 / 3
A familiar idea

A grid can hide a dependency graph.

1
2
4
3

Values are 1, 2 on top and 4, 3 below. Move to a larger neighbor in any of four directions.

Swipe when you’re ready
One new wrinkle

Right, down, then left.

1
2
3
4

1 → 2 → 3 → 4 visits all four squares. Row order alone would miss this dependency direction.

Same idea. One more small step.
Try a tiny example

Can these arrows make a cycle?

bigger
bigger
cannot circle back

Every move strictly increases the value. Returning to the starting value would require it to be larger than itself.

Same idea. One more small step.
Notice what matters

Each square stores its best continuation.

best larger neighbor
+ this square

Its answer is 1 plus the largest answer among bigger neighbors. A square with no bigger neighbor stores 1.

Same idea. One more small step.
Make it reusable

Longest-path DP on a hidden DAG.

value 4: length 1
value 1: length 4

Process dependencies from larger values toward smaller ones. The example’s answers become 1 at value 4, up to 4 at value 1.

Want to try a tiny question?
No pressure. Just try it.

If equal-valued moves are allowed, do we still have the same no-cycle proof?

You can also keep scrolling
1 / 6
Thinking in DP

One small step at a time.

This idea builds on these smaller ones. Pick any to revisit it here.

The dependency order is only a start.

A: 2
B: 5
C: waits

A takes 2 hours; B takes 5. Both can start now. C takes 3 and needs both finished.

1 / 5
A familiar idea

The cheapest arrival may use too many flights.

S→A: 5
S→B→A: 2
A→T: 1

S→A costs 5. S→B→A costs 2. A→T costs 1. We may take at most two flights.

Swipe when you’re ready
One new wrinkle

The cheaper A cannot continue legally.

A: 2, none left
A: 5, one left

It already used both flights. Keep the cost-5 arrival too: it still has one flight left and reaches T for 6.

Same idea. One more small step.
Try a tiny example

Give each flight budget its own layer.

0 flights
1 flight
2 flights

Layer e stores the cheapest arrival using at most e flights. Advance one layer by trying every flight once.

Same idea. One more small step.
Notice what matters

Read yesterday. Write today.

previous layer
next layer

Copy the previous distances before relaxing edges. Reading fresh updates would accidentally allow multiple flights within one layer.

Same idea. One more small step.
Make it reusable

Bellman–Ford becomes budgeted DP.

city
flights allowed

At most k stops means at most k + 1 flights. The extra layer makes even a cyclic route network safe to evaluate.

Want to try a tiny question?
No pressure. Just try it.

Why can’t one cheapest price per city describe the whole problem?

You can also keep scrolling
1 / 6
Thinking in DP

One small step at a time.

This idea builds on these smaller ones. Pick any to revisit it here.

A road gives a refund.

A→B: 2
A→C: 5
C→B: −4

A→B costs 2. A→C costs 5. C→B refunds 4, so that edge costs −4.

1 / 3
You've reached the end of these ideas.Try them without the labels