[프로그래머스]구명보트

박민하·2022년 10월 21일
0

python 문제

목록 보기
48/49

문제

✅ 코드

1. 오답

answer = 0
    people.sort(reverse=True)
    while len(people) > 1:
        if limit - people[0] < people[-1]:
            del people[0]
            answer +=1
        elif people[0] == people[-1]:
            if len(people) == 2:
                break
            else:
                del people[-1]
                del people[0]
                answer += 1
        else:
            for i in range(1, len(people)):
                if limit - people[0] >= people[i]:
                    del people[i]
                    del people[0]
                    answer += 1
    answer += 1
    return answer

2. 다른 사람의 코드1

from collections import deque

def solution(people, limit):
    answer = 0
    deq = deque(sorted(people))
    while len(deq):
        if len(deq) == 1:
            answer += 1
            break
        if deq[0] + deq[-1] <= limit:
            deq.pop()
            deq.popleft()
        else:
            deq.pop()
        answer += 1
    return answer

3. 다른 사람의 코드2

def solution(people, limit) :
    answer = 0
    people.sort()

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

☑️ 핵심코드

1. deque(데크): 양방향 큐

profile
backend developer 🐌

0개의 댓글