728x90
https://school.programmers.co.kr/learn/courses/30/lessons/150369
# 실패
def solution(cap, n, deliveries, pickups):
answer = 0
box, box2 = 0, 0
i, pos = n-1, n-1
if max(deliveries) == 0 and max(pickups) == 0:
return 0
while True:
if i == 0:
answer += (pos+1)*2
break
if box < cap and deliveries[i]>0:
d = cap-box if deliveries[i]>=cap-box else deliveries[i]
box += d
deliveries[i] -= d
if box2 < cap and pickups[i]>0:
p = cap-box2 if pickups[i]>=cap-box2 else pickups[i]
box2 += p
pickups[i] -= p
if box == cap and box2 == cap:
answer += (pos+1)*2
box = 0
box2 = 0
pos = i
i -= 1
return answer
# 시간초과
def solution(cap, n, deliveries, pickups):
answer = 0
list_d = []
list_p = []
pos_d, pos_p = n, n
cap_d, cap_p = cap, cap
while True:
if deliveries == []:
break
d = deliveries.pop()
if d > cap_d:
deliveries.append(d-cap_d)
cap_d = 0
elif d > 0:
cap_d -= d
if cap_d == 0:
list_d.append(pos_d)
pos_d = len(deliveries)
cap_d = cap
if cap_d == cap and d == 0:
pos_d -= 1
if cap_d < cap:
list_d.append(pos_d)
while True:
if pickups == []:
break
p = pickups.pop()
if p > cap_p:
pickups.append(p-cap_p)
cap_p = 0
elif p > 0:
cap_p -= p
if cap_p == 0:
list_p.append(pos_p)
pos_p = len(pickups)
cap_p = cap
if cap_p == cap and p == 0:
pos_p -= 1
if cap_p < cap:
list_p.append(pos_p)
while True:
if list_d == [] or list_p == []:
break
d = list_d.pop(0)
p = list_p.pop(0)
if d > p:
answer += (d*2)
else:
answer += (p*2)
if list_d != []:
answer += (sum(list_d)*2)
if list_p != []:
answer += (sum(list_p)*2)
return answer
# 정답
def solution(cap, n, deliveries, pickups):
answer = 0
box_d, box_p = 0, 0
for i in range(n-1, -1, -1):
box_d += deliveries[i]
box_p += pickups[i]
while box_d > 0 or box_p > 0:
box_d -= cap
box_p -= cap
answer += (i+1)*2
return answer
너무 어렵다. 아이셔 왕창 먹었다.
반응형
'전.py' 카테고리의 다른 글
[python] 프로그래머스 두 큐 합 같게 만들기 (0) | 2023.01.17 |
---|---|
[python] 프로그래머스 이모티콘 할인행사 (0) | 2023.01.13 |
[python] 프로그래머스 개인정보 수집 유효기간 (2) | 2023.01.11 |
[python] 프로그래머스 성격 유형 검사하기 (0) | 2023.01.11 |
[python] 효율적인 화폐 구성 (0) | 2022.07.21 |