Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e5c6fb

Browse files
feat: Lv4 무지의 먹방 라이브 풀이 추가
1 parent 6b24880 commit 9e5c6fb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎level-4/무지의-먹방-라이브.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - ryong9rrr
4+
function solution(food_times, k) {
5+
const total = food_times.reduce((a, b) => a + b)
6+
if (total <= k) {
7+
return -1
8+
}
9+
10+
// stack으로 풀기
11+
const stack = food_times.map((time, i) => [time, i + 1]).sort((a, b) => b[0] - a[0])
12+
13+
let prev = 0
14+
while (stack.length > 0 && k >= 0) {
15+
const time = stack[stack.length - 1][0]
16+
const acc = (time - prev) * stack.length
17+
if (k < acc) {
18+
break
19+
}
20+
stack.pop()
21+
k -= acc
22+
prev = time
23+
}
24+
25+
const result = stack
26+
.reverse()
27+
.map((item) => item[1])
28+
.sort((a, b) => a - b)
29+
return result[k % result.length]
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /