最长上升子序列O(n^2) java


import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {

			int n = sc.nextInt();
			int arr[] = new int[n];
			int f[] = new int[n];
			f[0] = 1;
			int ans = 0;
			for (int i = arr.length - 1; i >= 0; i--) {
				arr[i] = sc.nextInt();
			}
			for (int i = 0; i < arr.length; i++) {
				int m = 0;
				for (int j = 0; j < i; j++) {
					if (f[j] > m && arr[j] <= arr[i]) {
						m = f[j];
					}
				}
				f[i] = m + 1;
				if (ans < f[i]) {
					ans = f[i];
				}
			}
			System.out.println(ans);

		}

	}

}

点赞