Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 하스스톤
- 미앤아이
- 발더스모드
- 누룽지소금빵
- 송리단
- 발더스3
- 메일우유
- 노노그램
- 토이프로젝트
- 알고리즘테스트
- 버즈2프로
- 눈알빠지겠네
- 3d퍼즐
- 밥먹고
- 취미
- 나쫌
- javascript
- DIY
- LeetCode
- 뜨아거
- 천등
- 코딩테스트
- 맛집
- 잠실새내
- 메탈퍼즐
- 게임
- 코테
- 바질토마토뭐시기
- 발더스게이트
- 서울제빵소
Archives
- Today
- Total
.Zzumbong
[leetCode/JS] 2225. Find Players With Zero or One Losses 본문
coding test/leetCode
[leetCode/JS] 2225. Find Players With Zero or One Losses
쭘봉 2022. 11. 28. 10:11문제 설명
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.
matchs = [winner, loser] 형식으로 param이 온다.
answer[0]은 절대 진적 없는 winner들만.
answer[1]은 1번만 진 플레이어들만.
sort((a,b)=>a-b) 오름차순 하여 return answer 하면 된다.
입출력 예
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].
Example 2:
Input: matches = [[2,3],[1,3],[5,4],[6,4]]
Output: [[1,2,5,6],[]]
Explanation:
Players 1, 2, 5, and 6 have not lost any matches.
Players 3 and 4 each have lost two matches.
Thus, answer[0] = [1,2,5,6] and answer[1] = [].
Constraints
1 <= matches.length <= 105
matches[i].length == 2
1 <= winneri, loseri <= 105
winneri != loseri
- All
matches[i]
are unique.
내 솔루션
- Object를 사용해도 비슷하지만, 중복을 고려한다면 Set이 편하다.
- loser[] 를 만들어서 nerverLost[]나 oneLost[]에 add하기 전에 비교하였다.
- 마지막에
Set
을Array
로 변환하여 리턴하면 끝.
/**
* @param {number[][]} matches
* @return {number[][]}
*/
var findWinners = function(matches) {
const neverLost = new Set();
const oneLost = new Set();
const losers = new Set();
for(let i = 0; i < matches.length; i++) {
const [winner, loser] = matches[i];
if(oneLost.has(loser)){
oneLost.delete(loser);
} else if(!losers.has(loser)) {
oneLost.add(loser);
}
losers.add(loser);
if(!losers.has(winner)) {
neverLost.add(winner);
}
if(neverLost.has(loser)){
neverLost.delete(loser);
}
}
return [[...neverLost].sort((a,b)=>a-b), [...oneLost].sort((a,b)=>a-b)]
};
감상평
- 아침에 첫 문제로 풀기 딱 좋은 가볍고 재미있는 문제였다.
'coding test > leetCode' 카테고리의 다른 글
[leetCode/JS] 1207. Unique Number of Occurrences (0) | 2022.11.30 |
---|---|
[leetCode/JS] 380. Insert Delete GetRandom O(1) (0) | 2022.11.29 |
[leetCode/JS] 446. Arithmetic Slices II - Subsequence (0) | 2022.11.27 |
[leetCocd/JS] 907. Sum of Subarray Minimums (0) | 2022.11.25 |
[leetCode/JS] 79. Word Search (0) | 2022.11.24 |
Comments