Download presentation
Presentation is loading. Please wait.
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.