java求最大递增子序列算法
求最大递增子序列:
package com.test.algorithm; import java.util.Arrays; /** * Created by Administrator on 2022/10/12. */ public class MaxIncrSub { /* 求最大递增子序列 */ public static void main(String[] args) { int[] a = {1,3,2,0,4}; int[] b = new int[a.length]; int len; int n = a.length; b[0] = 1; for(int i=1;i<n;i++){ len = 0; for(int j=0;j<i;j++){ if(a[j]<=a[i]&&len<b[j]){ len = b[j]; } } b[i] = len + 1; } System.out.println(Arrays.toString(b)); int maxLen = getMaxLen(b); System.out.println(maxLen); } private static int getMaxLen(int[] b) { int temp = 0; for(int i=0;i<b.length;i++){ if(b[i] > temp){ temp = b[i]; } } return temp; } }
输出结果:
[1, 2, 2, 1, 3]
3