Code - Longest Harmonious Subsequence

<문제>

<답안>

/**
 * @param {number[]} nums
 * @return {number}
 */
var findLHS = function(nums) {
    var answer = 0;
    let fmap = new Map();
    for (let num of nums) {
        fmap.set(num, (fmap.get(num) || 0) + 1)
    }
    // console.log(fmap);
    
    for (let [key, val] of fmap) {
        console.log("key : " + key + " val : " + val);
        if (fmap.has(key+1)){
            answer = Math.max(answer, val + fmap.get(key+1))

        }
    }
    return answer;
};

좋은 웹페이지 즐겨찾기