题解 | 最长上升子序列(一)

最长上升子序列(一)

https://www.nowcoder.com/practice/5164f38b67f846fb8699e9352695cd2f

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 给定数组的最长严格上升子序列的长度。
     * @param arr int整型一维数组 给定的数组
     * @return int整型
     */
    public int LIS (int[] arr) {
        // write code here
        if (arr.length <= 1)return arr.length;

        int res = 0;
        //dp[i]表示以i为结尾的最长上升子序列的长度
        int[] dp = new int[arr.length];
        Arrays.fill(dp, 1);
        for (int i = 0; i < dp.length; i++) {
            for (int j = 0; j < i; j++) {
                if (arr[j] < arr[i]) {
                    dp[i] = Math.max(dp[i], 1 + dp[j]);
                    res = Math.max(dp[i],res);
                }
            }
        }
        return res;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务