Created
December 4, 2017 19:32
-
-
Save saionaro/f83365ad94937d62a665c4c0023943bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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