天天看点

1299--最长上升子序列 动态规划(java)

Submit Statistic Discuss 
Problem Description
一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1, ai2, ..., aiK),这里1<= i1 < i2 < ... < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 8)等等。这些子序列中最长的长度是4,比如子序列(1, 3, 5, 8)。

你的任务,就是对于给定的序列,求出最长上升子序列的长度。 
Input
输入的第一行是序列的长度N (1 <= N <= 1000)。第二行给出序列中的N个整数,这些整数的取值范围都在0到10000。 
Output
最长上升子序列的长度。 
Sample Input
7
1 7 3 5 9 4 8
Sample Output
4
           

dp[i] = 前面value值中小于当前value值的dp最大值+1。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a[] = new int[n];
		for(int i = 0;i<n;i++) {
			a[i] = sc.nextInt();
		}
        int[] dp = new int[a.length];
        int res = 0;

        for (int i = 0; i < a.length; i++) {
            dp[i] = 1;
            for (int j = 0; j <= i; j++) {
                if (a[j] < a[i]) {
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
            }
            res = Math.max(res, dp[i]);
        }

        System.out.println(res);
    }
}