Exercises — DP problems — rod cutting, egg drop, DP on trees
3.7.14 · D4· Coding › Algorithm Paradigms › DP problems — rod cutting, egg drop, DP on trees
Yeh page ek self-test hai. Problem padho, khud try karo, phir collapsible solution kholо. Har problem parent note aur Dynamic Programming ki core idea pe build karti hai: recursive structure dhundho, phir usse baar-baar compute karna band karo.
Yahaan ki har recurrence teen prerequisites pe tiki hai jo tumhe open rakhni chahiye: Recursion and Memoization (kyun ek baar solve karke store karna better hai), Tree Traversal (DFS) (woh order jisme hum tree nodes visit karte hain), aur Binary Search (jo ek galat shortcut aur ek sahi optimization ke roop mein aata hai).
Level 1 — Recognition
Exercise 1.1 — Subproblem ka naam bolo
Recall Solution
Yeh pure pattern recognition hai — koi arithmetic nahi.
- (a) ek chhoti rod. Hum ek baar left pe cut karte hain, kamate hain, aur length ki rod ke saath reh jaate hain — wahi problem, chhota input.
- (b) ek chhota (eggs, floors) pair. Ek drop ke baad, ya toh egg toota (kam eggs, neeche ke kam floors) ya bacha (same eggs, upar ke kam floors). Dono tarah se state shrink hoti hai.
- (c) ek subtree. Kyunki tree mein no cycles hote hain, har child ka subtree ek self-contained problem hai jiska answer hum parent pe combine karte hain.
Ek unifying sentence: "Subproblems ka smallest set kya hai jiske answers milaakar poore problem ka answer milta hai?"
Exercise 1.2 — Base case padho
Recall Solution
- . Zero floors ke saath test karne ko kuch nahi hai — threshold question already answer ho chuka hai, isliye hum zero drops karte hain. (Jab koi floor nahi hai toh eggs ki count irrelevant hai.)
- . Single egg ke saath, toot jaana information ka permanent loss hai. Agar tum gamble karo aur floor se drop karo aur woh toot jaye, toh tum floors test nahi kar sakte. Isliye single safe strategy hai neeche se floor by floor chadhna, worst case drops.
Level 2 — Application
Exercise 2.1 — Rod-cutting ka crank ghumaо
Recall Solution
WHAT hum karte hain: table left to right fill karo, har entry every leftmost piece length try karti hai.
| computation | first cut | ||
|---|---|---|---|
| 0 | — | 0 | — |
| 1 | 2 | 1 | |
| 2 | 5 | 2 | |
| 3 | 7 | 1 | |
| 4 | 10 | 2 |
WHY pe 10 aata hai: leftmost piece lena (price 5) ek length-2 rod chhod deta hai jiska worth hai, total — poori rod mein bechne se zyaada.
Reconstruct: pe pehla cut hai → length 2 ka piece. Remaining rod length ; pe pehla cut hai → ek aur length-2 piece. Pieces , revenue .
Exercise 2.2 — Tree ka crank ghumaо
Recall Solution
WHAT hum karte hain: leaves pehle process karo (post-order DFS), kyunki parent ko apne children ke answers chahiye.
- Node 3 (leaf): , .
- Node 2 (child 3): . .
- Node 1 (child 2): . .
- Node 0 (child 1): . .
Answer .
WHY 8: node 0 ko exclude karna (state ) node 1 ke subtree ko apna best karne deta hai (jo tha ki 1 ko exclude karo aur node 2 lo). Node 0 ko include karna node 1 ko exclude karne par majboor karta hai, lekin node 2 ab free hai — set weight ke saath jeetta hai.
Level 3 — Analysis
Exercise 3.1 — Egg drop mein choices compare karo
Recall Solution
Har candidate first-drop floor ke liye, do branches hain: toot jaye → , bach jaye → . Hum unka lete hain (adversary), 1 add karte hain, phir woh choose karte hain jo sabse chhota total de.
| break | survive | |||
|---|---|---|---|---|
| 1 | 0 | 3 | 4 | |
| 2 | 1 | 3 | 4 | |
| 3 | 2 | 2 | 3 | |
| 4 | 3 | 3 | 4 | |
| 5 | 4 | 4 | 5 | |
| 6 | 5 | 5 | 6 |
Minimum pe hai: , optimal first drop floor 3 se.
WHY do branches ko best balance karta hai: ek achha first drop "cost if it breaks" aur "cost if it survives" ko equalize karta hai. pe dono branches ka cost hai, isliye na break na survive case dusre se worse ho sakta hai — adversary ke paas koi lever nahi hai.
Exercise 3.2 — Rod cut path reconstruct karo
Recall Solution
Reconstruction cut array ko ek linked list ki tarah walk karta hai:
- Length pe: → length ka piece emit karo. Remaining .
- Length pe: → length ka piece emit karo. Remaining .
- Length → ruk jao.
Pieces , revenue . Yeh se match karta hai, reconstruction confirm karta hai.
Level 4 — Synthesis
Exercise 4.1 — Recurrence invent karo: rod cutting with a per-cut cost
Recall Solution
WHAT badalta hai: jab bhi hum split karte hain tab cut charge karna. Isse model karo ek baar pay karke jab bhi hum leftmost piece lete hain aur rod bacha rehta hai.
= length ke liye best net revenue. Leftmost length try karte hue:
- agar toh poori cheez bech do, koi cut nahi: value ;
- agar toh ek cut karo (fee ) aur continue karo: value .
Apply ():
- .
- .
- .
- .
WHY answer 10 se gir ke 9 ho gaya: purana winner gross kamata tha lekin ek cut chahiye, net — ab sirf poori rod mein bechne ke barabar (koi cut nahi). Cut fee ne split karne ka faayda khatam kar diya.
Exercise 4.2 — States invent karo: tree with "at most one child kept"
Recall Solution
WHAT constraint hai: MWIS (no adjacent pair) ke unlike, yahaan ek picked parent ek picked child ki allow karta hai. Isliye parent ki value depend karti hai ki usne kitne children pe apna allowance "kharch" kiya.
Har node ke liye do states define karo:
- = ke subtree mein best jab pick nahi kiya gaya — children unconstrained hain.
- = ke subtree mein best jab pick kiya gaya — at most ek child bhi pick ho sakta hai.
pick nahi kiya: har child apna best choose karta hai, koi coupling nahi:
pick kiya: default yeh hai ki har child leta hai (pick nahi). Lekin hum ek child ko "upgrade" karne ki permission rakhte hain. Child ko upgrade karne se milta hai agar woh positive ho. Sabse profitable single upgrade lo:
g(v,1)=\text{base}+\max\Big(0,\ \max_{c}\big(g(c,1)-g(c,0)\big)\Big).$$ Answer $=\max\big(g(\text{root},0),\,g(\text{root},1)\big)$; phir bhi $O(n)$ hai kyunki har edge ek baar touch hoti hai. WHY correct: subtrees disjoint hain isliye values add hote hain; sirf ek "upgrade" allowance ka coupling hai, jo best single child gain choose karke capture hota hai.Level 5 — Mastery
Exercise 5.1 — Dual "forward" egg-drop aur ek complexity claim
Recall Solution
WHAT dual kehta hai: "min drops for floors" ke bajaye pucho "with drops, kitne floors clear kar sakta hoon?" badhao jab tak na ho jaye.
eggs ke liye, aur (ek egg floors drops mein clear karta hai). Yeh telescope karke triangular numbers deta hai .
| 12 | 78 |
| 13 | 91 |
| 14 | 105 ≥ 100 ✓ |
Sabse chhota jisme ho woh hai .
WHY yeh faster hai: naive table cells fill karta hai, har ek inner scan karta hai → . Forward version table fill karta hai per cell ke saath, aur sirf hai chhote ke liye — yahaan versus . Toh hum ke bajaye constant-time steps karte hain.
Exercise 5.2 — Greedy ek star pe exactly kyun fail karta hai
Recall Solution
(a) Greedy: centre ko leta hai (weight , single heaviest). Centre saare leaves ke adjacent hai, isliye har leaf ab forbidden hai. Greedy total .
(b) DP optimum do MWIS states se, root = centre:
- Har leaf : , .
- Centre included: .
- Centre excluded: .
- Answer → set = chaar leaves.
(c) Greedy kab haar jaata hai: jab bhi ek heavy node ke neighbours collectively usse outweigh kar lein. Formally, node ko pick karna ek galti hai agar . Greedy locally sahi hai ( per node) lekin globally galat (). Do-state DP include-vs-exclude ko poore subtree mein ek saath compare karta hai, isliye woh kabhi isme nahi phasta. Yahi wajah hai Greedy Algorithms Knapsack Problem par fail karte hain: locally best pick ek globally better combination block kar sakti hai.
Recall Self-check: problem → state definition match karo
Rod cutting state ::: = length ki rod se best revenue (ek dimension). Egg drop state ::: = eggs, floors ke saath min worst-case drops (do dimensions). Tree MWIS states ::: aur = exclude / include hone par best subtree weight. Egg drop dual ::: = eggs aur drops ke saath max floors clearable.