LeetCode-300-最长上升子序列

思路

  • 记录每个位置的最长上升子序列
  • 对于每个位置,扫描这个位置之前的所有位置
  • 如果当前位置 > 扫描到的位置:更新当前的最长上升子序列

代码

class Solution {
    public int lengthOfLIS(int[] nums) {
        if(nums.length == 0)return 0;
        int[] dp = new int [nums.length];
        Arrays.fill(dp, 1);
        int res = 0;
        for(int i = 0;i<nums.length;i++) {
            for(int j = 0;j<i;j++) {
                if(nums[i] > nums[j]) {
                    dp[i] = Math.max(dp[j] + 1, dp[i]);
                }
            }
            res = Math.max(res, dp[i]);
        }
        return res;
    }
}

Algorithms   LeetCode      动态规划

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!

LeetCode-15-三数之和 上一篇
LeetCode-440-字典序的第K小数字 下一篇