https://programmers.co.kr/learn/courses/30/lessons/86053
코딩테스트 연습 - 금과 은 운반하기
어느 왕국에 하나 이상의 도시들이 있습니다. 왕국의 왕은 새 도시를 짓기로 결정하였습니다. 해당 도시를 짓기 위해서는 도시를 짓는 장소에 금 a kg과 은 b kg이 전달되어야 합니다. 각 도시에는
programmers.co.kr
이진 탐색으로 풀면 된다.
나의 풀이
def solution(a, b, g, s, w, t):
start = 0
end = int(1e9*1e5*2*2)
answer = end
while start<=end:
mid = (start+end)//2
all_gold, all_silver, all_total = 0,0,0
for i in range(len(g)):
now_gold, now_silver, now_total, now_time = g[i], s[i], w[i], t[i]
if mid//now_time%2 ==1:
count = (mid//now_time//2)+1
else:
count = mid//now_time//2
all_gold += now_gold if (now_gold < now_total*count) else now_total*count
all_silver += now_silver if (now_silver < now_total*count) else now_total*count
all_total += now_gold + now_silver if (now_gold + now_silver < now_total*count) else now_total*count
if all_gold >= a and all_silver >= b and all_total >= a+b :
# 시간 더 줄이기
answer = min(answer,mid)
end = mid-1
else:
# 시간 늘리기
start = mid+1
return answer
'Programming > Programmers' 카테고리의 다른 글
[프로그래머스] 경주로 건설 (Python) (0) | 2021.11.10 |
---|---|
[프로그래머스] 합승 택시 요금 (Python) (0) | 2021.11.10 |
[프로그래머스] 불량 사용자 (Python) (0) | 2021.11.09 |
[프로그래머스] 보석 쇼핑 (Python) (0) | 2021.11.09 |
[프로그래머스] 순위 (Python) (0) | 2021.11.08 |
댓글