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

最长上升子序列(一)

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

另一种思路,先排序,然后求最长公共子序列。这里怎么说:

2 3 2 1 5 -》排序后:1 2 2 3 5,

求 1 2 2 3 4与2 3 2 1 5的最长公共子序列,得到的即是最长上升子序列,这里排序是为了满足上升的需求

时间复杂度为 n^2 空间复杂度 n^2

import java.util.*;


public class Solution {

    public int LIS (int[] arr) {
        int length = arr.length;
        int[] time = new int[length];
        System.arraycopy(arr, 0, time, 0, length);
        Arrays.sort(time);
        int[][] lcs = new int[length+1][length+1];
        for(int i = 1; i <= length; i++){
            for(int j = 1; j <= length; j++){
                if(arr[i - 1] == time[j - 1]) lcs[i][j] = lcs[i-1][j-1] + 1;
                else lcs[i][j] = Math.max(lcs[i-1][j], lcs[i][j-1]);
            }
        }
        return lcs[length][length];
    }
}

全部评论

相关推荐

06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
07-02 18:09
门头沟学院 Java
苍穹外卖和谷粒商城这俩是不是烂大街了,还能做吗?
想去重庆的鸽子在吐槽:你不如把这俩做完自己搞明白再优化点再来问 何必贩卖焦虑
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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