最长上升子序列

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定数组的最长严格上升子序列的长度。
     * @param arr int整型一维数组 给定的数组
     * @return int整型
     */
    public int LIS (int[] arr) {
        // write code here
        if(arr.length == 0){
            return 0;
        }
        // dp 表示以它为底的最长序列的长度
        int res = 1;
        int[] dp = new int[arr.length];
        dp[0] = 1;// 初始化
        for(int i=1; i<dp.length; i++){
            int max=1;
            for(int j=0;j<i;j++){
                if(arr[i]>arr[j]){
                    // 说明可以查看,到达最大长度
                    max = Math.max(dp[j]+1, max);
                }
            }
            dp[i] = max;
            res = Math.max(max, res);

        }
        return res;
    }
}

  

posted @ 2023-10-28 22:37  樱圃  阅读(2)  评论(0编辑  收藏  举报