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 |