[Level2] 구명보트

Quesuemon·2021년 3월 26일
0

코딩테스트 준비

목록 보기
14/111

🛠 문제

https://programmers.co.kr/learn/courses/30/lessons/42885


👩🏻‍💻 해결 방법

우선 people 리스트를 정렬해 주고, 맨앞 사람과 맨뒤 사람을 보트에 태웠을 때 limit을 초과하면 맨뒤 사람만 보트에 태워준다

소스 코드

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

0개의 댓글