chyam

[프로그래머스 Lv2, python]- 구명보트 본문

프로그래머스/LV2

[프로그래머스 Lv2, python]- 구명보트

chyam_eun 2024. 12. 28. 21:56

def check(people,limit,cnt):
    while(1):
        if len(people)!=0:
            if people[0]+people[-1]<=limit and len(people)>1: #길이가 2넘고 앞과 뒤를 더한게 limit보다 적으면 없애기
                people.pop()
                people.pop(0)
                cnt+=1
            else:
                people.pop()
                cnt+=1
        else:
            break
    return cnt
def solution(people, limit):
    cnt = 0
    people.sort()
    cnt=check(people,limit,cnt)
    return cnt

위에거는 처음풀이인데 효율성은 통과못한 풀이임,,,

def check(people,limit,cnt):
    st=0 #앞부분 인덱스
    fin=len(people)-1 #뒷부분 인덱스
    while(1):
        if people[st]+people[fin]<=limit: #최소의 수와 최대의 수가 limit보다 적을때 st증가
            st+=1
        cnt+=1 #숫자 증가시키고 fin 감소시키기
        fin-=1
        if st>fin: # 앞부분이 뒷부분보다 커지면 종료
            break        
    return cnt
def solution(people, limit):
    cnt = 0
    people.sort()
    cnt=check(people,limit,cnt)
    return cnt