Python Little Exercise 12

Source: Internet
Author: User

Give you an integer sequence a1,a2,a3,..., An, please modify (can not delete, only modify) the least number, so that the series strictly monotonically increment. The sequence is stored in the list L, and you can directly use the l,l length less than 100000. Note: It is important to ensure that the modified sequence remains a series of integers and cannot be modified into decimals. For example: l=[1,3,2], then output 1

This topic, is to find a longest increment of the sub-sequence (that is not to modify the maximum number), and this sub-order needs to meet a condition is that the difference between the two elements is greater than the value of their position in the array l, because only so that the elements between them can be changed into a strict increment of the sequence = , and then subtracting the length of our array from the longest increment of the subsequence is the answer.

For example, we have a sequence of [1,5,6,3,4,5,2,7,8], and we have found that its oldest sequence should be [3,4,5,7,8], the value of this subsequence remains unchanged in the original array, and the other data can be changed to a strictly incrementing

How to find this sequence, we can directly find an array of the largest sub-sequence size can be, using dynamic programming, take m[i] to the end of the maximum number of l[i] the size of the sub-series, we have its size should be in 1 (set up a new sequence itself) or its front

The maximum number of the maximal sub-sequences that satisfy the condition is to satisfy the difference between elements greater than the subscript of the element, i.e. {M[i] = max{m[:j]} + 1, L[i]-l[k] >= i-k, K for traversing M subscript}

The code is as follows

defDP (L): Size=Len (L) RSL= [0] *size Rsl[0]= 1 forIinchRange (1, size): forJinchRange (i):ifL[i]-l[j] >= i-j andRsl[i] < Rsl[j] + 1:#dynamic planning to get resultsRsl[i] = Rsl[j] + 1ifRsl[i] = = 0:#If no conditions are met, a sequenceRsl[i] = 1returnMax (RSL) L= [1,5,6,3,4,5,2,7,8]Print(Len (L)-DP (L))

Python Little Exercise 12

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.