# Dynamic Programming

### Elementary Recurrences

10.1. Up to ${\displaystyle k}$ steps in a single bound! A child is running up a staircase with ${\displaystyle n}$ steps and can hop between 1 and ${\displaystyle k}$ steps at a time. Design an algorithm to count how many possible ways the child can run up the stairs, as a function of ${\displaystyle n}$ and ${\displaystyle k}$. What is the running time of your algorithm?

10.2. Imagine you are a professional thief who plans to rob houses along a street of ${\displaystyle n}$ homes. You know the loot at house ${\displaystyle i}$ is worth ${\displaystyle m_{i}}$, for ${\displaystyle 1\leq i\leq n}$, but you cannot rob neighboring houses because their connected security systems will automatically contact the police if two adjacent houses are broken into. Give an efficient algorithm to determine the maximum amount of money you can steal without alerting the police.

10.3. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible mixes (1s,2s,3s) of scoring add up to a given ${\displaystyle n}$. For ${\displaystyle n}$ = 5 there are four possible solutions: (5, 0, 0), (2, 0, 1), (1, 2, 0), and (0, 1, 1).

10.4. Basketball games are a sequence of 2-point shots, 3-point shots, and 1-point free throws. Give an algorithm that computes how many possible scoring sequences add up to a given ${\displaystyle n}$. For ${\displaystyle n}$ = 5 there are thirteen possible sequences, including 1-2-1-1, 3-2, and 1-1-1-1-1.

10.5. Given an ${\displaystyle s*t}$ grid filled with non-negative numbers, find a path from top left to bottom right that minimizes the sum of all numbers along its path. You can only move either down or right at any point in time.
(a) Give a solution based on Dijkstra’s algorithm. What is its time complexity as a function of ${\displaystyle s}$ and ${\displaystyle t}$?
(b) Give a solution based on dynamic programming. What is its time complexity as a function of ${\displaystyle s}$ and ${\displaystyle t}$?

### Edit Distance

10.6. Typists often make transposition errors exchanging neighboring characters,

such as typing “setve” for “steve.” This requires two substitutions to fix under the conventional definition of edit distance.

Incorporate a swap operation into our edit distance function, so that such neigh-

boring transposition errors can be fixed at the cost of one operation.

10.7. Suppose you are given three strings of characters: ${\displaystyle X}$, ${\displaystyle Y}$, and ${\displaystyle Z}$, where ${\displaystyle |X|=n}$, ${\displaystyle |Y|=m}$, and ${\displaystyle |Z|=n+m}$. ${\displaystyle Z}$ is said to be a shuffle of ${\displaystyle X}$ and ${\displaystyle Y}$ iff ${\displaystyle Z}$ can be formed by interleaving the characters from ${\displaystyle X}$ and ${\displaystyle Y}$ in a way that maintains the left-to-right ordering of the characters from each string.
(a) Show that cchocohilaptes is a shuffle of chocolate and chips, but chocochilatspe is not.
(b) Give an efficient dynamic programming algorithm that determines whether ${\displaystyle Z}$ is a shuffle of ${\displaystyle X}$ and ${\displaystyle Y}$. (Hint: the values of the dynamic programming matrix you construct should be Boolean, not numeric.)

10.8. The longest common substring (not subsequence) of two strings ${\displaystyle X}$ and ${\displaystyle Y}$ is the longest string that appears as a run of consecutive letters in both strings. For example, the longest common substring of photograph and tomography is ograph.
(a) Let ${\displaystyle n=|X|}$ and ${\displaystyle m=|Y|}$. Give a ${\displaystyle \Theta (nm)}$ dynamic programming algorithm for longest common substring based on the longest common subsequence/edit distance algorithm.

(b) Give a simpler ${\displaystyle \Theta (nm)}$ algorithm that does not rely on dynamic programming.

10.9. The longest common subsequence (LCS) of two sequences ${\displaystyle T}$ and ${\displaystyle P}$ is the longest sequence ${\displaystyle L}$ such that ${\displaystyle L}$ is a subsequence of both ${\displaystyle T}$ and ${\displaystyle P}$. The shortest common supersequence (SCS) of ${\displaystyle T}$ and ${\displaystyle P}$ is the smallest sequence ${\displaystyle L}$ such that both ${\displaystyle T}$ and ${\displaystyle P}$ are a subsequence of ${\displaystyle L}$.
(a) Give efficient algorithms to find the LCS and SCS of two given sequences.
(b) Let ${\displaystyle d(T,P)}$ be the minimum edit distance between ${\displaystyle T}$ and ${\displaystyle P}$ when no substitutions are allowed (i.e., the only changes are character insertion and deletion). Prove that $\displaystyle d(T, P) = |SCS(T, P)| − |LCS(T, P)|$ where ${\displaystyle |SCS(T,P)|(|LCS(T,P)|)}$ is the size of the shortest SCS (longest LCS) of ${\displaystyle T}$ and ${\displaystyle P}$.

10.10. Suppose you are given ${\displaystyle n}$ poker chips stacked in two stacks, where the edges of all chips can be seen. Each chip is one of three colors. A turn consists of choosing a color and removing all chips of that color from the tops of the stacks. The goal is to minimize the number of turns until the chips are gone.
For example, consider the stacks ${\displaystyle (RRGG,GBBB)}$. Playing red, green, and then blue suffices to clear the stacks in three moves. Give an ${\displaystyle O(n^{2})}$ dynamic programming algorithm to find the best strategy for a given pair of chip piles.

10.11

10.12

10.13

10.14

10.15

10.16

10.17

10.18

10.19

10.20

10.21

10.22

10.23

10.24

10.25

10.26

10.27

10.28

10.29

10.30

10.31

10.32

10.33

10.34

10.35

10.36

10.37

10.38

### Interview Problems

10.39

10.40

10.41

Back to Chapter List