-
Notifications
You must be signed in to change notification settings - Fork 2
/
B.js
32 lines (26 loc) · 1.58 KB
/
B.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
/**
* "B. Выборы в США" {@link "https://contest.yandex.ru/contest/28970/problems/B/}
*
* Чему будут равны значения чисел, если сложить между собой все те, что имеют одинаковый цвет.
* Напишите, пожалуйста, программу, которая выводит результат.
*
* @param { Array<[string, number] } surnameWithVotes список - фамилия кандидата, за которого отдают
* голоса выборщики этого штата, и количество выборщиков, отдавших голоса за этого кандидата.
*
* @return { string[] } Выведите фамилии всех кандидатов в лексикографическом порядке, затем, через
* пробел, количество отданных за них голосов.
*/
function calcVotesInTheElections(surnameWithVotes) {
const votesBySurname = new Map();
for (let index = 0; index < surnameWithVotes.length; index++) {
const [surname, votesCount] = surnameWithVotes[index];
votesBySurname.set(surname, BigInt(votesBySurname.get(surname) || 0) + BigInt(votesCount));
}
const surnames = [...votesBySurname.keys()].sort((a, b) => a.localeCompare(b));
return surnames.map((color) => `${color} ${votesBySurname.get(color)}`);
}
function inputProcessing(lines) {
const surnameWithVotes = lines.map((line) => line.split(' '));
return calcVotesInTheElections(surnameWithVotes);
}
export default inputProcessing;