Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS330 Discussion 4 Spring 2017.

Similar presentations


Presentation on theme: "CS330 Discussion 4 Spring 2017."β€” Presentation transcript:

1 CS330 Discussion 4 Spring 2017

2 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)

3 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 𝐷.

4 DP Practice Problem 3 Suppose you’re planning a bus route. There are 𝑛 possible stops along the route. The 𝑖th stop 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 𝑖th stop, you cannot select the π‘–βˆ’1th or 𝑖+1th stop. Write an algorithm to determine the maximum number of people you can pick up. Report its runtime as well.

5 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 𝐡.

6 DP Practice Problem 5a Consider the following two-player game: There is a line of coins with positive integer values 𝑣 1 , 𝑣 2 … 𝑣 𝑛 ( 𝑣 1 is the value of the left-most coin, 𝑣 2 is 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)

7 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.


Download ppt "CS330 Discussion 4 Spring 2017."

Similar presentations


Ads by Google