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

最长上升子序列(一)

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];
    }
}

全部评论

相关推荐

哈哈哈看得出来讨论很沸腾了,很高兴了属于是&nbsp;大家快说说截止目前还有哪家互联网大厂不是双休?(在说谁,在说谁)一起避坑!!
不正经草莓:听红薯来的同事讲,他们之前周六下午6点多就走了,一天也没啥事儿到手6k多工资,要我也加班查看图片
投递小红书等公司6个岗位 > 小红书取消大小周
点赞 评论 收藏
分享
FieldMatching:看成了猪头顾问,不好意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务