728x90
반응형
https://programmers.co.kr/learn/courses/30/lessons/42891?language=python3
최종 코드
from heapq import heappush, heappop
def solution(food_times, k):
if sum(food_times) <= k: return -1
hp = []
for i in range(1, len(food_times)+1):
heappush(hp,[food_times[i-1], i])
eat = 0
length = len(food_times)
prev_eat = 0
while True:
if eat + (hp[0][0] - prev_eat)*length >= k: break
now_eat = heappop(hp)[0]
eat += (now_eat - prev_eat)*length
length -= 1
prev_eat = now_eat
hp.sort(key = lambda x:x[1])
k -= eat
return hp[k%length][1]
풀이 과정
from heapq import heappush, heappop
def solution(food_times, k):
if sum(food_times) <= k: return -1
hp = []
for i in range(1, len(food_times)+1):
heappush(hp,[food_times[i-1], i])
eat = 0
length = len(food_times)
prev_eat = 0
while True:
if eat + (hp[0][0] - prev_eat)*length >= k: break
now_eat = heappop(hp)[0]
eat += (now_eat - prev_eat)*length
length -= 1
prev_eat = now_eat
hp.sort(key = lambda x:x[1])
k -= eat
return hp[k%length][1]
오답 노트
- 정확성만 통과한 코드
#정확성 통과 코드
def solution(food_times, k):
answer = 0
if k >= sum(food_times): return -1
while k > -1:
for i in range(len(food_times)):
if food_times[i] > 0:
food_times[i] -= 1
k -= 1
if k == -1: return i+1
return answer
1시간 23분동안 작성한 코드
#무지의 먹방 라이브 실패코드
def solution(food_times, k):
answer = 0
if k >= sum(food_times): return -1
remain = []
done = 0
while k > len(food_times)-done:
remain.clear()
eat1 = k//(len(food_times)-done)
eat2 = k%(len(food_times)-done)
for i in range(len(food_times)):
if food_times[i] > eat1:
food_times[i] -= eat1
remain.append(i)
else:
eat2 += abs(food_times[i]-eat1)
food_times[i] = 0
done += 1
k = eat2
if k == 0:
return remain[0]+1
return remain[k%len(remain)]+1
참고
- 7. 블록 게임 ->
728x90
반응형
'코테 노트 > 프로그래머스' 카테고리의 다른 글
Level 2 문자열 압축 <2020 KAKAO BLIND RECRUITMENT > Python 3 (0) | 2021.09.05 |
---|---|
Level 3 징검다리 건너기 <2019 카카오 인턴십> Python3 (0) | 2021.09.05 |
Level 3 길 찾기 게임 <2019 KAKAO BLIND RECRUITMENT> Python 3 (0) | 2021.09.02 |
Level 2 후보키 <2019 KAKAO BLIND RECRUITMENT> Python 3 (0) | 2021.09.02 |
Level 2 오픈채팅방 <2019 KAKAO BLIND RECRUITMENT> Python 3 (0) | 2021.09.02 |