Title Link: http://www.pythontip.com/coding/code_oj_case/36
Give you an integer list L, such as l=[2,-3,3,50], for a discontinuous sub-sequence of l, so that it and the maximum, the output maximum sub-sequence of the and. The definition of a discontinuous sub-sequence here is that any adjacent two numbers in the subsequence are not contiguous in the original sequence. For example, for l=[2,-3,3,50], output 52 (parse: It is clear that the list's maximum discontinuous subsequence is [2,50]).
DP first copies the elements of the L sequence and then compares the first two elements to determine the optimal solution assignment to dp[1].
1 # l=[2,-3,3,50] 2 dp = list (L)3 dp[1] = max (dp[1], dp[0])4 for in range (2 , Len (L)):5 dp[i] = max (max (Dp[i], dp[i-1]), dp[i-2]+l[i])6 Print(Dp[len (L)-1])
[Pythontip] Maximum discontinuous subsequence