Introduction to the dynamic planning algorithm for the longest non-descending subsequence

Source: Internet
Author: User

Description

A sequence has N numbers: A [1], A [2],…, A [N], and obtain the length of the longest non-descending subsequence.

Train of Thought Analysis

 

Write code

Recursive code seems to be easier to understand, but the code with a circular method is more concise.

Recursive Method

Show you code:

 

# Include
 
  
Using namespace std; int dp [6]; int max (int a, int B) {return (a> B )? A: B;} int dp_fun (int * A, int I, int num) {if (I = 0) {dp [I] = 1; return max (dp [I], dp_fun (A, I + 1, num);} else {dp [I] = 1; for (int j = 0; j
  
   
Dp [I]) dp [I] = dp [j] + 1;} if (I = num) return dp [I]; else return max (dp [I], dp_fun (A, I + 1, num) ;}} int main () {int A [] = {5, 3, 4, 8, 6, 7}; cout <
   
    

 

Use the Loop Method

Show you code:

 

#include 
     
      using namespace std;int lis(int A[], int n){    int *d = new int[n];    int len = 1;    for(int i=0; i
      
       d[i])                d[i] = d[j] + 1;                        if(d[i]>len) len = d[i];    }    delete[] d;    return len;}int main(){    int A[] = {        5, 3, 4, 8, 6, 7    };    cout<
       
        

 

 

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.