728x90

https://school.programmers.co.kr/learn/courses/30/lessons/150369

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

# 실패

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

 

너무 어렵다. 아이셔 왕창 먹었다. 

반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기