프로그래머스/[코테]Level2
[Python] 구명보트
숨겨진 오징어
2020. 11. 6. 21:51
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