AcWing895.最长上升子序列

AcWing895.最长上升子序列_第1张图片

import java.io.*;

public class Main{
	static int n;
	static int N = 1010;
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static int dp[] = new int[N];
	static int data[] = new int[N];
	public static void main(String[] args) throws IOException{
		n = Integer.parseInt(in.readLine());
		String[] init = in.readLine().split(" ");
		for(int i = 1;i <= n;i ++) data[i] = Integer.parseInt(init[i -1]);
		
		int res = -1;
		//dp[i]:以data[i] 结尾的最长上升子序列
		for(int i = 1;i <= n;i ++) {
		    //无论如何,自己的最长子序列算1
			dp[i] = 1;
			for(int j = 1;j < i;j ++) {
				if(data[i] > data[j]){
				    //1~i-1会有多个dp[j] 要找一个最大的
				    dp[i] = Math.max(dp[i], dp[j] + 1);
				}
			}
			
			res = Math.max(res, dp[i]);
		}
		System.out.println(res);
	}
}

你可能感兴趣的:(java,算法)