Dynamic Programming
Prev: combinatorial-search Next: np-completeness
10.11 Exercises
Elementary Recurrences
10-1. [3] A child is running up a staircase with steps and can hop between and steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of and . What is the running time of your algorithm?
10-2. [3] You plan to rob houses along a street. The loot at house is worth , but you cannot rob neighboring houses. Give an efficient algorithm to determine the maximum amount of money you can steal.
10-3. [5] Basketball scoring involves 1-, 2-, and 3-point shots. Give an algorithm that computes how many possible combinations add up to a given . E.g., for there are four mixes: , , , .
10-4. [5] Compute how many scoring sequences (order matters) add up to a given using 1-, 2-, and 3-point scores. For , there are thirteen such sequences.
10-5. [5] Given an grid of non-negative numbers, find a path from top-left to bottom-right that minimizes the sum of numbers along the path. You can move only down or right.
-
(a) Solve with Dijkstra’s algorithm. Analyze time complexity.
-
(b) Solve with dynamic programming. Analyze time complexity.
Edit Distance
10-6. [3] Incorporate a swap operation (transposition of adjacent characters) into the edit distance function so that such errors cost one operation.
10-7. [4] Given strings , , and such that , , and , determine if is a shuffle of and (i.e., maintains character order).
-
(a) Show that
cchocohilaptes
is a shuffle ofchocolate
andchips
. -
(b) Give a DP algorithm to decide if is a shuffle.
10-8. [4] Find the longest common substring (not subsequence) of two strings and .
-
(a) Give a DP algorithm based on edit distance.
-
(b) Give a simpler algorithm without DP.
10-9. [6] Let and be two sequences. Let and denote the longest common subsequence and shortest common supersequence.
-
(a) Give efficient algorithms to compute both.
-
(b) Prove , where is the edit distance with only insertions and deletions.
10-10. [5] You are given two stacks of poker chips, each chip colored. In a move, you choose a color and remove all top chips of that color. Goal: remove all chips in fewest moves. Give an DP algorithm.
Greedy Algorithms
10-11. [4] Let be programs with sizes and a disk of size .
-
(a) Does greedy selection by smallest maximize number of programs stored?
-
(b) Does greedy selection by largest maximize disk usage?
10-12. [5] Given coin denominations :
-
(a) Show greedy does not always minimize number of coins (e.g., ).
-
(b) Give an efficient DP algorithm to compute minimum coins to make change for .
10-13. [5] Count the number of distinct ways to make change of units with denominations .
-
(a) How many ways to make 20 from ?
-
(b) Give an efficient algorithm and analyze its time.
10-14. [6] Given jobs each with processing time and deadline , show that scheduling by earliest deadline yields a feasible schedule if one exists.
Number Problems
10-15. [3] Rod cutting problem: maximize value from cutting a rod of length , given prices for each length. Give a DP solution.
10-16. [5] Maximize value of an arithmetic expression with terms by parenthesizing. E.g., .
10-17. [5] Compute the smallest number of perfect squares that sum to . Give an efficient DP algorithm.
10-18. [5] Find the largest sum of a contiguous subarray in .
10-19. [5] Partition suitcases of weight into two subsets of equal total weight.
10-20. [6] Solve the subset sum problem (knapsack): find a subset of summing to in time.
10-21. [6] Solve the integer partition problem: partition into two subsets with equal sum in time.
10-22. [5] Given numbers on a circle, find the max contiguous sum along any arc.
10-23. [5] Given positions to break a string, determine break order to minimize total cost. DP algorithm in time.
10-24. [5] Given dictionary of strings (each of length ), find best way to encode string of length using fewest substrings. Design algorithm.
10-25. [5] Chess match: 24 games. Champion keeps title in case of tie. Games have probabilities depending on color.
-
(a) Write a recurrence for champion’s retention probability.
-
(b) Implement DP algorithm.
-
(c) Analyze time complexity.
10-26. [8] Egg drop: find floor where egg breaks. Let be min drops with eggs and floors.
-
(a) Show .
-
(b) Show .
-
(c) Give recurrence and DP algorithm.
Graph Problems
10-27. [4] Given grid with “bad” intersections:
-
(a) Give example where no path exists.
-
(b) algorithm to find any path avoiding bad cells.
-
(c) algorithm to find shortest path.
10-28. [5] In same grid setting, count number of safe paths of length . Algorithm must run in .
10-29. [5] Stack boxes where each box must be strictly smaller than the one below in width, height, and depth. Maximize stack height.
Design Problems
10-30. [4] Books of fixed height on shelves of length . Show greedy packing minimizes number of shelves. Analyze time.
10-31. [6] Books of varying height. Shelf height is max height of any book on it.
-
Show greedy doesn’t minimize height.
-
Give DP algorithm and analyze time.
10-32. [5] Given linear keyboard of keys, and starting finger positions, type string of length with minimal total finger movement. DP algorithm.
10-33. [5] You know the future price of stock over days. One transaction per day. Maximize profit under constraints.
10-34. [8] Given compressed string with no spaces:
-
(a) Use dict() to determine if can be split into words. time.
-
(b) If dict is a set of words of max length , give an efficient solution.
10-35. [8] Two-player digit game: starting from -digit number , each player alternates taking first or last digit to maximize their score. Give optimal DP strategy for player 1.
10-36. [6] Maximum contiguous subarray sum:
-
Give algorithm.
-
Then give DP algorithm. Partial credit: divide-and-conquer.
10-37. [7] Given string over -symbol alphabet and a multiplication table, decide if you can parenthesize to evaluate to symbol . Multiplication is non-commutative and non-associative. Polynomial time algorithm in and .
10-38. [6] Given keys and query probabilities , and cost (left), (right), build BST with optimal expected query cost.
Interview Problems
10-39. [5] Given coin denominations, find the minimum number of coins to make a given amount.
10-40. [5] Given array of numbers (positive, negative, or zero), find indices and to get max sum in subarray .
10-41. [7] Can you construct a string using cut-out letters from a magazine, where cutting one letter also removes the reverse side? Assume a function gives you the reverse letter/position. Give an algorithm to decide if target string can be formed.
Prev: combinatorial-search Next: np-completeness