Skip to content

Commit 41a2de7

Browse files
committed
[Chore] 답안 참고해서 수정 (#1)
1 parent bbcf3e3 commit 41a2de7

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed
Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
11
# food_times 배열
22
# k초가 매개변수
3+
# 답 참고해서 리팩...ㅠㅠ
34
import heapq
45

56
food_times1 = list(map(int, input().split()))
67
k1 = int(input())
78

8-
99
def solution(food_times, k):
10-
answer = -1
1110
if sum(food_times) <= k:
12-
return answer
13-
while k >= 0:
14-
for i in range(len(food_times)):
15-
if food_times[i] == 0:
16-
continue
17-
else:
18-
k -= 1
19-
heap = []
20-
food_times[i] -= 1
21-
heapq.heappush(heap, food_times[i])
22-
if k == -1:
23-
answer = i + 1
24-
return answer
11+
return -1
12+
13+
# 우선순위큐(최소힙)에다가 삽입
14+
# 음식 번호와 시간을 튜플형태로 삽입, 시간을 기준으로 최소힙 완성됨
15+
q = []
16+
for i in range(len(food_times)):
17+
heapq.heappush(q, (food_times[i], i + 1))
18+
19+
# q [(1초, 2번), (2초, 3번), (3초, 1번)]
20+
21+
food_num = len(food_times) # 남은 음식의 개수
22+
previous = 0 # 이전 음식을 먹는데 걸리는 시간
23+
24+
while True:
25+
# 먹는데 걸리는 시간 = 음식을 먹는 시간 * 남은 음식의 개수
26+
time = (q[0][0]-previous)*food_num
27+
28+
# 중단 시간 >= 먹는데 걸리는 시간
29+
if k >= time:
30+
k -= time
31+
food_num -= 1
32+
previous = heapq.heappop(q)[0]
33+
34+
# 중단 시간 <= 먹는데 걸리는 시간
35+
else:
36+
i = k % food_num
37+
q.sort(key=lambda x: x[1]) # 음식 번호로 정렬
38+
return q[i][1]
2539

2640

2741
solution(food_times1, k1)

0 commit comments

Comments
 (0)