Loud and Rich LeetCode solution in JavaScript

1
var loudAndRich = function (richer, quiet) {
2
const map = new Map();
3
for (const [rich, poor] of richer) {
4
map.set(poor, (map.get(poor) || new Set()).add(rich));
5
}
6

7
const memo = new Map();
8
const getQuietest = (person) => {
9
if (memo.has(person)) return memo.get(person);
10
const richerList = map.get(person);
11
let min = quiet[person];
12
let quietest = person;
13
if (!richerList) {
14
memo.set(person, quietest);
15
return quietest;
16
}
17
for (const rich of richerList) {
18
if (quiet[getQuietest(rich)] < min) {
19
min = quiet[getQuietest(rich)];
20
quietest = getQuietest(rich);
21
}
22
}
23
memo.set(person, quietest);
24
return quietest;
25
};
26
const answer = [];
27
for (let i = 0; i < quiet.length; i++) {
28
answer.push(getQuietest(i));
29
}
30
return answer;
31
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0