N

(Leet Code JS)Longest Increasing Subsequence 본문

Leet Code 알고리즘

(Leet Code JS)Longest Increasing Subsequence

naeunchan 2022. 8. 20. 16:12
728x90
반응형

https://leetcode.com/problems/longest-increasing-subsequence/

 

Longest Increasing Subsequence - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

/**
 * @param {number[]} nums
 * @return {number}
 */
var lengthOfLIS = function(nums) {
    const length = nums.length;
    const dp = Array.from({length}, () => 0);
    let answer = 0;
    
    for(let i = 0; i < length; i++){
        if(dp[i] === 0){
            dp[i] = 1;
        }
        
        for(let j = 0; j < length; j++){
            if(nums[j] < nums[i]){
                if(dp[i] < dp[j] + 1){
                    dp[i] = dp[j] + 1;
                }
            }
        }
        
        answer = Math.max(answer, dp[i]);
    }
    
    return answer;
};
728x90
반응형