diff --git "a/Programmers/Lv. 2/\353\215\224 \353\247\265\352\262\214.js" "b/Programmers/Lv. 2/\353\215\224 \353\247\265\352\262\214.js" new file mode 100644 index 0000000..a37f11d --- /dev/null +++ "b/Programmers/Lv. 2/\353\215\224 \353\247\265\352\262\214.js" @@ -0,0 +1,29 @@ +const mixScoville = (food1, food2) => { + return food1 + food2 * 2; +}; + +const checkScoville = (arr, K) => { + let result = true; + arr.forEach((el) => { + if (el < K) result = false; + }); + return result; +}; + +function solution(scoville, K) { + let answer = 0; + + scoville.sort((a, b) => a - b); + if (scoville[0] >= K) return 0; + + while (!checkScoville(scoville, K)) { + const [f1, f2] = [scoville.shift(), scoville.shift()]; + const mixValue = mixScoville(f1, f2); + scoville.unshift(mixValue); + answer++; + if (scoville.length === 1 && scoville[0] < K) return -1; + scoville.sort((a, b) => a - b); + } + + return answer; +}