-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path인턴쉽.js
79 lines (67 loc) · 1.79 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
solution(
["muzi", "ryan", "frodo", "neo"],
[
"muzi frodo",
"muzi frodo",
"ryan muzi",
"ryan muzi",
"ryan muzi",
"frodo muzi",
"frodo ryan",
"neo muzi",
]
);
solution(
["joy", "brad", "alessandro", "conan", "david"],
[
"alessandro brad",
"alessandro joy",
"alessandro conan",
"david alessandro",
"alessandro david",
]
);
solution(["a", "b", "c"], ["a b", "b a", "c a", "a c", "a c", "c a"]);
function solution(friends, gifts) {
const friendsMap = new Map();
for (const friend of friends) {
friendsMap.set(friend, {
givens: {},
received: {},
giftScore: 0,
giftsToReceive: 0,
});
}
for (const gift of gifts) {
const [giver, receiver] = gift.split(" ");
const giverEntry = friendsMap.get(giver);
const receiverEntry = friendsMap.get(receiver);
if (giverEntry) {
giverEntry.givens[receiver] = (giverEntry.givens[receiver] || 0) + 1;
giverEntry.giftScore += 1;
}
if (receiverEntry) {
receiverEntry.received[giver] = (receiverEntry.received[giver] || 0) + 1;
receiverEntry.giftScore -= 1;
}
}
for (const [friend1, friend1Data] of friendsMap.entries()) {
for (const [friend2, friend2Data] of friendsMap.entries()) {
if (friend1 === friend2) continue;
const giftsFromFriend2 = friend1Data.received[friend2] || 0;
const giftsToFriend2 = friend1Data.givens[friend2] || 0;
if (
giftsToFriend2 > giftsFromFriend2 ||
(giftsToFriend2 === giftsFromFriend2 &&
friend1Data.giftScore > friend2Data.giftScore)
) {
friend1Data.giftsToReceive += 1;
}
}
}
let answer = 0;
for (const friendData of friendsMap.values()) {
answer = Math.max(answer, friendData.giftsToReceive);
}
return answer;
}