Created
September 11, 2019 14:20
-
-
Save webhacking/7fe4cf14210a72d7aac26bbcadcff3c0 to your computer and use it in GitHub Desktop.
3번 문제 풀이한거 ㅇㅇ
This file contains hidden or 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
/* | |
지방의 수는 3 이상 100,000 이하인 자연수입니다. | |
각 지방에서 요청하는 예산은 1 이상 100,000 이하인 자연수입니다. | |
총 예산은 지방의 수 이상 1,000,000,000 이하인 자연수입니다. | |
*/ | |
function solution(budgets, M) { | |
var answer = 0, | |
mid = 0, | |
minimum = 0, | |
maximum = 100000, | |
total = budgets.reduce((a, b) => a + b); | |
budgets.sort(); | |
if (total < M) { | |
return budgets[budgets.length - 1]; | |
} | |
while (true) { | |
total = 0; | |
mid = (maximum + minimum) / 2; | |
for (let i = 0; i < budgets.length; i++) { | |
total += budgets[i] < mid ? budgets[i] : mid*(budgets.length- 1 - (i - 1)); | |
if (budgets[i] > mid) { | |
break | |
} | |
} | |
if (mid === answer) { | |
return Math.floor(answer); | |
} | |
total > M ? maximum = mid : minimum = mid; | |
answer = mid; | |
} | |
console.log('budgets', budgets, M); | |
return Math.floor(answer); | |
} | |
// console.log(solution([120, 110, 140, 150], 485)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment