1번 풀이

from queue import deque
def solution(people, limit):
    people = deque(sorted(people))
    with_boat,alone_boat = [],[]
    
    t=0
    while people:
        if len(people)==1:
            alone_boat.append(people.popleft())
            break
        left, with_append = people.popleft(), False
        while people:
            right = people.pop()
            if left+right>limit:
                alone_boat.append(right)
            else:
                with_boat.append((left,right))
                with_append=True
                break
        if not with_append:
            alone_boat.append(left)
        print(f'{t}번째의 alone_boat : {alone_boat},   with_boat : {with_boat}')
        t += 1
    answer = len(alone_boat)+len(with_boat)
    return answer

 

2번 풀이

def solution(people, limit):
    people.sort()
    answer, a, b = len(people), 0, len(people)-1
    while a<b:
        if people[a]+people[b]<=limit:
            a+=1
            answer-=1
        b-=1
    return answer

'프로그래머스 > [코테]Level2' 카테고리의 다른 글

[Python] 카펫  (0) 2020.11.06
[Python] 문자열 압축  (0) 2020.10.24
[Python] 큰 수 만들기  (0) 2020.10.23
[Python] 주식가격  (0) 2020.10.23
[Python] 위장  (0) 2020.10.23

+ Recent posts