[Pythontip] Maximum discontinuous subsequence

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.