๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“š Study Note/LeetCode

2225. Find Players With Zero or One Losses

by Jellll_y 2022. 11. 28.

2225. Find Players With Zero or One Losses

Medium


You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.

Return a list answer of size 2 where:

  • answer[0] is a list of all players that have not lost any matches.
  • answer[1] is a list of all players that have lost exactly one match.

The values in the two lists should be returned in increasing order.

Note:

  • You should only consider the players that have played at least one match.
  • The testcases will be generated such that no two matches will have the same outcome.

 

Example 1:

Input: matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
Output: [[1,2,10],[4,5,7,8]]
Explanation:
Players 1, 2, and 10 have not lost any matches.
Players 4, 5, 7, and 8 each have lost one match.
Players 3, 6, and 9 each have lost two matches.
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].

 

Constraints:

  • 1 <= matches.length <= 105
  • matches[i].length == 2
  • 1 <= winneri, loseri <= 105
  • winneri != loseri
  • All matches[i] are unique.

์ด ๋ฌธ์ œ๋Š” ํ•œ๋ฒˆ๋„ ์ง„์ ์ด ์—†๋Š” ์‚ฌ๋žŒ๋“ค๊ณผ ๋”ฑ ํ•œ ๋ฒˆ๋งŒ ์ง„ ์‚ฌ๋žŒ๋“ค์„ ์ฐพ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

map์„ ์ด์šฉํ•ด์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฒ˜์Œ์—๋Š” winner, loser ๋‘˜ ๋‹ค ๋”ฐ๋กœ map์„ ๋งŒ๋“ค์–ด์„œ ํ’€์—ˆ์ง€๋งŒ

๊ทธ๊ฒƒ๋ณด๋‹ค ๋‘˜์ด ํ•ฉ์ณ์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒŒ ๋” ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ๋ฆฌํŒฉํ„ฐ๋ง ํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์กฐ๊ฑด์— ๋งž๋Š” ์‚ฌ๋žŒ๋“ค์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์ด๊ธด์‚ฌ๋žŒ์€ ๊ฐ’์„ 0 

์ง„ ์‚ฌ๋žŒ์€ ๊ณ„์†ํ•ด์„œ 1์„ ๋”ํ•œ๊ฐ’์„ ์ €์žฅํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. 

 

/**
 * @param {number[][]} matches
 * @return {number[][]}
 */
var findWinners = function(matches) {
    const total = new Map()
    
    for(const match of matches){
        const [win, lose] = match
        
        total.set(win, (total.get(win)||0))
        total.set(lose, (total.get(lose)||0)+1)
    }
    
    const winner = [] , loser = []
    
    for(const [key, value] of total){
        if(value === 0) winner.push(key)
        if(value === 1) loser.push(key)
    }
    
    winner.sort((a,b)=>a-b)
    loser.sort((a,b)=>a-b)
    
    return [winner, loser]
};

'๐Ÿ“š Study Note > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

209. Minimum Size Subarray Sum  (0) 2022.12.28
346. Moving Average from Data Stream  (0) 2022.12.26
967. Numbers With Same Consecutive Differences  (0) 2022.11.26
49. Group Anagrams  (0) 2022.11.26
216. Combination Sum III  (0) 2022.11.24