-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-shoePairing.js
55 lines (46 loc) · 1.12 KB
/
05-shoePairing.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
// Challenge #5: 👢 Shoe Pairing.
/**
* @param {{ type: 'I' | 'R', size: number }[]} shoes
* @returns {number[]} Available shoes
*/
function organizeShoes(shoes) {
const bootsCount = {};
for (const { type, size } of shoes) {
const counts = bootsCount[size] || { I: 0, R: 0 };
counts[type]++;
bootsCount[size] = counts;
}
const result = [];
Object.entries(bootsCount).forEach(([size, { I, R }]) => {
const pairs = Math.min(I, R);
result.push(...Array(pairs).fill(Number(size)));
});
return result;
}
const shoes = [
{ type: "I", size: 38 },
{ type: "R", size: 38 },
{ type: "R", size: 42 },
{ type: "I", size: 41 },
{ type: "I", size: 42 },
];
console.log(organizeShoes(shoes));
// [38, 42]
const shoes2 = [
{ type: "I", size: 38 },
{ type: "R", size: 38 },
{ type: "I", size: 38 },
{ type: "I", size: 38 },
{ type: "R", size: 38 },
];
console.log(organizeShoes(shoes2));
// [38, 38]
const shoes3 = [
{ type: "I", size: 38 },
{ type: "R", size: 36 },
{ type: "R", size: 42 },
{ type: "I", size: 41 },
{ type: "I", size: 43 },
];
console.log(organizeShoes(shoes3));
// []