Problem: A sequence has N numbers: A[1],A[2],...,A[N], find the length of the longest non-descending subsequence.
Abstract: Find the length of the longest non-decreasing subsequence of these N numbers.
Idea: Using dynamic programming recursion, if d(i) is the length of the longest non-descending subsequence of the first i numbers, then it depends on the length of d(1)...d(i-1), and find Get d(i), and you can find d(i+1)...n later. Its state transition equation is: d(i) = max{1, d(j)+1}, where j=1...i-1, A[j]<=A[i].
code show as below:
package test; /** * Dynamic programming to find the longest non-decreasing subsequence problem * @author yanghang * */ public class Dongtaiguihua { /** * longest increasing subsequence * @param a * @return */ public static int lis(int[] a) { // Count the longest non-descending subsequence of the current stage int len = 0; // original array length int n = a.length; // current state int[] d = new int[n]; for (int i = 0; i < n; i++) { d[i] = 1; for (int j = 0; j < i; j++) { //implementation of state transition equation if (a[j] <= a[i] && d[j] + 1 > d[i]) { d[i] = d[j] + 1; } } if (d[i] > len) { len = d [i]; } } return len; } public static void main(String[] args) { // TODO Auto-generated method stub int[] a = { 5, 3, 4, 8, 6, 7 }; System.out.println(lis(a)); } }