https://programmers.co.kr/learn/courses/30/lessons/42885
✅ Solution
- people을 내림차순으로 정렬한다.
- 가장 큰 요소(people[0]) 을 보트에 추가하고, 제일 작은 요소부터 무게를 넘지 않을 때까지 추가한다.
- len(people) >=2 일 때 반복
- 그리고 people에 요소가 남아있다면 보트 하나가 추가로 필요한 것이므로 answer += 1 해준다.
✅ Code
from collections import deque
def solution(people, limit):
idx, answer, weight = 0, 0, 0
people.sort(reverse=True)
people = deque(people)
while len(people) >= 2:
weight = 0
answer += 1
weight += people.popleft()
while people:
if weight + people[-1] <= limit:
weight += people.pop()
else:
break
if people:
answer += 1
return answer
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - n^2 배열 자르기 (Python) (0) | 2022.06.25 |
---|---|
[프로그래머스] Level 2 - 3xn 타일링 (Python) (0) | 2022.06.25 |
[프로그래머스] Level 2 - 영어 끝말잇기 (Python) (0) | 2022.06.24 |
[프로그래머스] Level 2 - 전력망을 둘로 나누기 (Python) (1) | 2022.06.24 |
[프로그래머스] Level 2 - 피로도 (Python) (0) | 2022.06.23 |