๐Ÿ“š Study/Baekjoon

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค | ํƒ์š•๋ฒ• | ๊ตฌ๋ช…๋ณดํŠธ

์œฐ๊ฐฑ 2025. 10. 8. 21:42

https://school.programmers.co.kr/learn/courses/30/lessons/42885?language=python3

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

SW๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ ํ‰๊ฐ€, ๊ต์œก์˜ Total Solution์„ ์ œ๊ณตํ•˜๋Š” ๊ฐœ๋ฐœ์ž ์„ฑ์žฅ์„ ์œ„ํ•œ ๋ฒ ์ด์Šค์บ ํ”„

programmers.co.kr

 

์‹œ๋„ 1 ๊ธฐ์กด ์ฝ”๋“œ - ์‹œ๊ฐ„ ์ดˆ๊ณผ

๋ชจ๋“  ์กฐํ•ฉ์„ ๊ณ ๋ คํ•ด์•ผ ํ•˜๋‚˜ ํ–ˆ์ง€๋งŒ (combination),,, ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์™€๋ฒ„๋ฆผ

from itertools import combinations
def solution(people, limit):
    answer = 0
    
    processed_pp = []
    for boat in combinations(people,2):
        boat_weight = 0
        for i, b in enumerate(boat):
            if i in processed_pp:
                continue
            boat_weight += b
        if boat_weight > limit:
            continue
        else:
            answer += 1
            for i, p in enumerate(people):
                if p in boat:
                    processed_pp.append(people[i])
                    
    # print(processed_pp)
    if processed_pp != people:
        answer += len(people) - len(processed_pp)
    
    return answer

 

์‹œ๋„2 ํˆฌํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•™์Šต 

def solution(people, limit):
    people.sort()
    
    answer = 0
    left, right = 0, len(people) - 1
    
    while left < right:
        if people[left] + people[right] == limit:
            left += 1; right -= 1; answer += 1
        elif people[left] + people[right] < limit:
            left += 1
        else:
            right -= 1
    
    answer += len(people) - answer*2
    return answer

 

์‹œ๋„ 3

๋ฌด๊ฑฐ์šด ๊ฒƒ๊ณผ ๊ฐ€๋ฒผ์šด ๊ฒƒ์„ ์–‘๋์œผ๋กœ ๋ฐฐ๋ถ„ํ•  ์ˆ˜ ์žˆ๋„๋ก sort ์ •๋ ฌ

def solution(people, limit):
    people.sort()  # ๋ชธ๋ฌด๊ฒŒ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    left, right = 0, len(people) - 1
    answer = 0

    while left <= right:
        # ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ + ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์„ ํƒœ์›Œ๋ณธ๋‹ค
        if people[left] + people[right] <= limit:
            left += 1  # ๋‘˜ ๋‹ค ํƒœ์›€
        right -= 1   # ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์€ ๋ฐ˜๋“œ์‹œ ํƒœ์›Œ์•ผ ํ•จ
        answer += 1  # ๋ณดํŠธ 1์ฒ™ ์‚ฌ์šฉ

    return answer