Skip to content

Instantly share code, notes, and snippets.

@saionaro
Created December 4, 2017 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saionaro/f83365ad94937d62a665c4c0023943bd to your computer and use it in GitHub Desktop.
Save saionaro/f83365ad94937d62a665c4c0023943bd to your computer and use it in GitHub Desktop.
const tests = [
[[3, 5, 6, 2, 1, 3, 6], 7],
[[5, 5, 7], 6],
[[4, 2, 1, 3, 7 ], 5],
[[9], 5],
[[1], 1],
[[1, 1, 4], 2],
[[1, 1, 4], 2],
[[9, 1, 3], 3],
[[67687, 667566, 4553334, 6566, 4332, 87886, 12321], 45456]
];
/**
* Отображает число в его часть, которая сойдет за "большинство"
* @param {Number} number Число для которого
* @return {Number}
*/
const getVotesByNum = number => ~~(number / 2) + 1;
/**
* Вычисляет минимальное количество людей для решения любого вопроса
* @param {Array} list Распределение групп
* @return {Number}
*/
const getGuysCount = list => Object.assign([], list)
.sort((a, b) => a - b) // Тут любой алгоритм сортировки
.slice(0, getVotesByNum(list.length))
.reduce((acc, elem) => acc + getVotesByNum(elem), 0);
tests.forEach(test => console.log (
`${test[0]} => ${getGuysCount(test[0])} (expected ${test[1]})`
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment