A contiguous subsequence of a list S is a subsequence made up of consecutive elements of S. For instance, if S is 5, 15, 30, 10, 5, 40, 10, then 15, 30, 10 is a contiguous subsequence but 5, 15, 40 is not. Give a linear-time algorithm for the following task: Input: A list of numbers, a1, a2, . . . , an. Output: The contiguous subsequence of maximum sum (a subsequence of length zero has sum zero). For the preceding example, the answer would be 10, 5, 40, 10, with a sum of 55. (Hint: For each j {1, 2, . . . , n}, consider contiguous subsequences ending exactly at position j.)
Read moreTextbook Solutions for Algorithms
Question
Suppose two teams, \(A\) and \(B\), are playing a match to see who is the first to win \(n\) games (for some particular \(n\)). We can suppose that \(A\) and \(B\) are equally competent, so each has a 50% chance of winning any particular game. Suppose they have already played \(i+j\) games, of which \(A\) has won \(i\) and \(B\) has won \(j\). Give an efficient algorithm to compute the probability that \(A\) will go on to win the match. For example, if \(i=n-1\) and \(j=n-3\) then the probability that \(A\) will win the match is 7/8, since it must win any of the next three games.
Solution
Step 1 of 2
The given problem can be solved by using dynamic programming. Let \(R(i, j)\) denote the required probability. Then,
\(R(i, j)=1\) for \(i=0, j>0\) means \(A\) has already won the match.
\(R(i, j)=1\) for \(i=0, j>0\) means \(B\) has already won the match.
\(R(i, j)=\frac{R(i-1, j)+R(i, j-1)}{2} \text { for } i>0, j>0\) means more games should be played and each team wins half the time.
full solution