Created
June 4, 2020 02:11
-
-
Save DongguemYoo/26ac9985a3772ca449e5ceab768424d2 to your computer and use it in GitHub Desktop.
[코딩테스트] 스택/큐 다리를 지나는 트럭.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
비교를 sum으로 하면 시간초과에 걸린다 | |
sum 은 O(n)이기 때문에 | |
def solution(bridge_length, weight, truck_weights): | |
answer = 0 | |
onbriged=[0 for i in range(bridge_length)] | |
currentWeight =0 | |
while len(truck_weights) != 0: | |
answer+=1 | |
if(currentWeight + truck_weights[0] <= weight): | |
currentWeight -= onbriged[0] | |
onbriged.pop(0) | |
currentWeight+=truck_weights[0]; | |
onbriged.append(truck_weights.pop(0)) | |
else: | |
currentWeight-=onbriged[0] | |
onbriged.pop(0) | |
if (currentWeight + truck_weights[0] <= weight): | |
currentWeight += truck_weights[0]; | |
onbriged.append(truck_weights.pop(0)) | |
else: | |
onbriged.append(0) | |
return answer+len(onbriged) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment