강의

멘토링

커뮤니티

Inflearn Community Q&A

tkddnr9612248455's profile image
tkddnr9612248455

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

8. Sinking Titanic (Greedy)

효율성

Written on

·

446

0

안녕하세요

저는 정렬을 사용하지 않고 min,max를 이용해서 풀었는데 정렬로 푸는게 더 효율적일까요?

코드 첨부합니다.

while squre:
    if len(squre)==1:
        boat+=1
        break
    if max(squre)+min(squre)>m:
        squre.remove(max(squre))
        boat+=1
    
    else:
        squre.remove(max(squre))
        squre.remove(min(squre))
        boat+=1
python코딩-테스트코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네. 정렬을 하고 left와 right로 하는게 효율성이 더 좋습니다.

remove 로 제거는 한다고는 하지만 max()자체가 시간복잡도가 O(n)인데 너무 많이 호출합니다.

tkddnr9612248455's profile image
tkddnr9612248455

asked

Ask a question