From 278d2b6ae3c4faae3720b49f33b731a7ec1f1a05 Mon Sep 17 00:00:00 2001 From: Kled Yu <83483378+1017yu@users.noreply.github.com> Date: Mon, 8 Jan 2024 22:07:01 +0900 Subject: [PATCH] =?UTF-8?q?[PGM]=20=EB=8D=94=20=EB=A7=B5=EA=B2=8C=20/=20Le?= =?UTF-8?q?vel=202=20/=2060=EB=B6=84+?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 통과 실패 --- .../\353\215\224 \353\247\265\352\262\214.js" | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 "Kled/\353\215\224 \353\247\265\352\262\214.js" diff --git "a/Kled/\353\215\224 \353\247\265\352\262\214.js" "b/Kled/\353\215\224 \353\247\265\352\262\214.js" new file mode 100644 index 0000000..fcdf854 --- /dev/null +++ "b/Kled/\353\215\224 \353\247\265\352\262\214.js" @@ -0,0 +1,28 @@ +function solution(scoville, K) { + let answer = 0; + + // 1. 오름차순 정렬 + scoville.sort((a, b) => a - b); + + // 2. 가장 낮은 스코빌 지수가 K 값보다 작다면 반복 + while (scoville[0] < K) { + let index = 0; + // 3. 가장 낮은 두 개의 음식 + const mixed = scoville.shift() + scoville.shift() * 2; + + // 4. scoville 배열에 삽입하기 위에 인덱스 찾기 + while (scoville[index] < mixed) index += 1 + + // 5. 배열에 삽입 + scoville.splice(index, 0, mixed) + + // 6. 횟수 추가 + answer += 1; + + // 7. 모든 음식을 섞을 때까지 조건을 만족하지 못한다면 -1 리턴 + if (scoville.length < 2 && scoville[0] < K) return -1; + } + + return answer; +} +