https://programmers.co.kr/learn/courses/30/lessons/42885
from collections import deque
def solution(people, limit):
people.sort()
people = deque(people)
answer = 0
while (people):
first = people.popleft()
i = len(people) - 1
while (people and i >= 0 and people[i] + first > limit):
i -= 1
people.pop()
answer += 1
if (i >= 0 and people[i] + first <= limit):
people.pop()
answer += 1
return answer
people
을 정렬후 deque
에 넣고 시작을 한다. while
문을 도는데 first
는 현재 가장 몸무게가 덜 나가는 인원이고 맨 끝에서 부터 역순으로 first
와 같이 탈수있는 인원을 찾는데 못타는 인원은 혼자서 밖에 탈수 없으니(first
보다 몸무게가 덜 나가는 인원은 없음) pop
시키면서 answer
를 증가시킨다. while
문을 돌고나서 if
문으로 first
와 탈수 있는 인원인지 체크를 하고 True
면 pop
시킨후 answer
를 증가시킨다. 이 while
문을 돌고나서 answer
를 리턴하면 끝.