-
Notifications
You must be signed in to change notification settings - Fork 0
/
가장 많이 받은 선물.js
55 lines (46 loc) · 1.25 KB
/
가장 많이 받은 선물.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class GiftPoint {
constructor() {
this.total = 0;
this.points = {};
}
}
function solution(friends, gifts) {
const giftPoints = Object.fromEntries(friends.map((name) => [name, new GiftPoint()]));
gifts.forEach((s) => {
const [start, end] = s.split(' ');
const startGiftPoint = giftPoints[start];
const endGiftPoint = giftPoints[end];
if (!startGiftPoint.points[end]) {
startGiftPoint.points[end] = 0;
}
startGiftPoint.points[end] += 1;
startGiftPoint.total += 1;
if (!endGiftPoint.points[start]) {
endGiftPoint.points[start] = 0;
}
endGiftPoint.points[start] -= 1;
endGiftPoint.total -= 1;
});
const giftCounts = friends.map((me) => {
let result = 0;
friends.forEach((friend) => {
// 본인 제외
if (me === friend) {
return;
}
// 주고받은 선물이 없거나 같을 때
if ((giftPoints[me].points[friend] ?? 0) === 0) {
if (giftPoints[me].total > giftPoints[friend].total) {
result += 1;
}
return;
}
// 내가 더 많은 선물을 줬을 때
if ((giftPoints[me].points[friend] ?? 0) > 0) {
result += 1;
}
});
return result;
});
return Math.max(...giftCounts);
}