본문 바로가기
DC 2

[자료구조] 운송 트럭 파이썬 (dict)

by YGSEO 2021. 4. 20.
728x90
def solution(max_weight, specs, names):
    answer = []
    answer.append(None)
    print(answer)
    specs = dict(specs)
    # print(specs)
    
    tmp = 0
    cnt = 1
    for n in names:
        tmp += int(specs[n])
        if tmp > max_weight:
            cnt += 1
            tmp = int(specs[n])
        
        
        # print(tmp, cnt)
    return cnt

처음에 pseudo 코드로 만들고 풀었을때

에러가 났던 부분은

if 문에서

tmp를 int(specs[n])이 아니라 0로 설정해서 안풀렸다

 

tmp에 새로운 무게를 추가했을때, max_weight을 초과하게 되면

tmp는 추가한 무게로 초기화 시켜주는 것이 중요했다.(0이 아니라)

 

그리고 cnt는 1부터 시작해야 한다는 점을 틀렸었다.

 

728x90

댓글