Created
April 22, 2022 16:13
-
-
Save xnuk/dbf705351e0b2751b04095fb174b2495 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 arr = [8, 9, 1, 2, 3] | |
const limit = 6 | |
const akamigSum = (arr: readonly number[], x: number) => | |
arr | |
.map(item => item + x) | |
.filter(item => item > 0) | |
.reduce((a, b) => a + b) | |
const fib = (arr: readonly number[], limit: number) => { | |
let xMin = -Math.max(...arr) | |
let xMax = 0 | |
while (xMin < xMax) { | |
const xMid = Math.floor((xMin + xMax) / 2) | |
const sum = akamigSum(arr, xMid) | |
if (sum === limit) return xMid | |
if (sum < limit) { | |
xMin = xMid + 1 | |
continue | |
} | |
if (sum > limit) { | |
xMax = xMid - 1 | |
continue | |
} | |
} | |
// 근처에 있는 x (xMin, xMax) 중 작은 거 | |
return xMin | |
} | |
console.log(fib(arr, limit)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment