
Dynamic Programming Practice Problems and Algorithms
Solving dynamic programming practice problems involving coin change, minimum jumps, bus route planning, and longest common subsequence. Enhance your problem-solving skills with algorithms to tackle these challenges effectively.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CS330 Discussion 4 Spring 2017
DP Practice Problem 1 Suppose you have an infinite supply of ? different types of coins which are worth ?1,?2, ??cents respectively. Write an ?(??) algorithm which given ?1,?2 ??, determines whether it is possible to make exactly ? cents worth of change using these coins, and if so, what the minimum number of coins needed is. (Note, we can do better than ?(??), but that s outside the scope of this discussion)
DP Practice Problem 2 In the minimum jumps to the end problem, you are given an ?- element integer array ?. A path ? = {?1,?2 ?? 1,?} is a valid path if for each ??, ?? ?? 1+ ?[?? 1] (where ?0= 1) Another way to think of it is, you start at element 1 of ?. If you re currently at element ?, you may take a jump to any element from ? + 1 to ? + D[?]. For example, if ? = {2,3,1,1,5}, you could use ? = 2,5 ?? ? = 3,4,5 but not ? = {3,5}. Here, the minimum jump count is 2. Write an algorithm to determine the minimum-length of any valid path, i.e. the minimum jumps to get to the end of ?.
DP Practice Problem 3 Suppose you re planning a bus route. There are ? possible stops along the route. The ?thstop has ??people who would ride the bus if it stopped there. Your goal is to choose some of these stops to maximize the number of people the bus can pick up. However, to make sure the bus makes it to its final destination on time, it can only stop at up to ?:1 ? < ? of these stops. Furthermore, it should not stop at two adjacent stops. So, if you select the ?thstop, you cannot select the ? 1thor ? + 1thstop. Write an algorithm to determine the maximum number of people you can pick up. Report its runtime as well.
DP Practice Problem 4: Given two arrays ? and ?, their longest common subsequence is the longest array ? such that the elements in ? exist in both ? and ?, and their order is the same. For example, if ? = 1,4,3,2,5 and ? = {1,6,3,4,2,7}, the longest common subsequence is {1,4,2}. {1,4,3,2} is not a valid subsequence because 3 does not come before 4 in ?. Write an algorithm to determine the length of the longest common subsequence of ? and ?.
DP Practice Problem 5a Consider the following two-player game: There is a line of coins with positive integer values ?1,?2 ??(?1is the value of the left-most coin, ?2is the value of the second left-most coin ??is the value of the right-most coin). Each player takes turns taking either the left-most or right-most remaining coin until all coins are gone. A player s score is the sum of the values of the coins he has taken, minus the sum of the values of the coins his opponent has taken. A greedy solution is to always take the larger of the two end coins on each turn. Find an instance of this game where the greedy solution is not optimal against an optimally playing opponent. (Hint: The greedy solution is the optimal solution if there are three or less coins)
DP Practice Problem 5b Consider the game from the previous slide. Using dynamic programming, write an algorithm which, given the current sequence of coins ?1,?2 ??in the form of the array ?, determines the score at the end of the game of the first player if both players play optimally, i.e. play to maximize their score at the end of the game. Report its runtime as well.