1. 程式人生 > >動規基礎,最長上升子序列

動規基礎,最長上升子序列

package com.ohere;

public class LongestIncreasingSubSet {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] test = {1,7,3,5,9,4,8,9,10};
		longestString(test);
		
	}
	public static void longestString(int [] str){
		int [] maxLength = new int[str.length];
		maxLength[0] = 1;
		for (int i = 1; i < str.length; i++){
			int tempLen = 0;
			for (int j = 0; j < i;j++){
				if (str[j] < str[i]){
					tempLen = Math.max(tempLen, maxLength[j]);
				}
			}
			maxLength[i] = tempLen + 1;
		}
		int result = 0;
		for (int x: maxLength){
			result = Math.max(result, x);
		}
		System.out.println(result);
	}
}