Bleeding edge

[LeetCode] 2225. Find Players With Zero or One Losses - 자바스크립트 0627 본문

코딩테스트 공부

[LeetCode] 2225. Find Players With Zero or One Losses - 자바스크립트 0627

codevil 2022. 6. 27. 13:40

https://leetcode.com/problems/find-players-with-zero-or-one-losses/

 

Find Players With Zero or One Losses - 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

1. hash map과 return 할 result를 만든다

    const hash = {}
    const result = [[], []]

2. 주어진 matches에서 갯수를 카운팅한다

    matches.map(([win, lose]) => {
        hash[win] = (hash[win] || 0); hash[lose] = (hash[lose] || 0) + 1
    })

3. key의 값이 0이면 result[0] 1이면 result[1]에 push후 return 한다

    for (let key in hash) {
        if (hash[key] === 0) {
            result[0].push(Number(key))
        } else if (hash[key] === 1) {
            result[1].push(Number(key))
        }
    }
    return result

전체 풀이

var findWinners = function (matches) {
    const hash = {}
    const result = [[], []]
    matches.map(([win, lose]) => {
        hash[win] = (hash[win] || 0); hash[lose] = (hash[lose] || 0) + 1
    })
    for (let key in hash) {
        if (hash[key] === 0) {
            result[0].push(Number(key))
        } else if (hash[key] === 1) {
            result[1].push(Number(key))
        }
    }
    return result
};