인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

comad's profile image
comad

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

8. Sinking Titanic (Greedy)

침몰하는 타이타닉 코드

Written on

·

246

0

이렇게 작성했는데 상관없나요?

N, M = map(int, input().split())
weight = list(map(int, input().split()))
weight.sort()

cnt = 0  
p1 = 0
p2 = N - 1
while p1 < p2:
    p2 = len(weight) - 1
    if weight[p1] + weight[p2] > M:
        weight.pop()
        cnt += 1
    else:
        weight.pop(0)
        weight.pop()
        cnt += 1
    # 한 명만 남은 경우 > 그대로 구명보트에 태워 구조
    if len(weight) == 1:    
        cnt += 1
        break
    # 짝이 맞아서 리스트에 아무것도 없는 경우 > break문 사용
    if weight == []:
        break
print(cnt)
python코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네. 잘 하신 코드입니다.

comad's profile image
comad

asked

Ask a question