Skip to content

Commit ca83551

Browse files
authored
Create 6.py
1 parent 20275de commit ca83551

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

11/6.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import heapq
2+
3+
def solution(food_times, k):
4+
# 전체 음식을 먹는 시간보다 k가 크거나 같다면 -1
5+
if sum(food_times) <= k:
6+
return -1
7+
8+
# 시간이 작은 음식부터 빼야 하므로 우선순위 큐를 이용
9+
q = []
10+
for i in range(len(food_times)):
11+
# (음식 시간, 음식 번호) 형태로 우선순위 큐에 삽입
12+
heapq.heappush(q, (food_times[i], i + 1))
13+
14+
sum_value = 0 # 먹기 위해 사용한 시간
15+
previous = 0 # 직전에 다 먹은 음식 시간
16+
length = len(food_times) # 남은 음식의 개수
17+
18+
# sum_value + (현재의 음식 시간 - 이전 음식 시간) * 현재 음식 개수와 k 비교
19+
while sum_value + ((q[0][0] - previous) * length) <= k:
20+
now = heapq.heappop(q)[0]
21+
sum_value += (now - previous) * length
22+
length -= 1 # 다 먹은 음식 제외
23+
previous = now # 이전 음식 시간 재설정
24+
25+
# 남은 음식 중에서 몇 번째 음식인지 확인하여 출력
26+
result = sorted(q, key =lambda x: x[1]) # 음식의 번호 기준으로 정렬
27+
return result[(k - sum_value) % length][1]

0 commit comments

Comments
 (0)