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<