[Programmers] ν”Όλ‘œλ„

Updated:

ν”Όλ‘œλ„

ν”Όλ‘œλ„λ₯Ό ν΄λ¦­ν•˜λ©΄ λ°”λ‘œ μ΄λ™ν•œλ‹€.

λ˜μ „μ˜ μ΅œλŒ€ κ°œμˆ˜κ°€ 8개이기 λ•Œλ¬Έμ— μˆœμ—΄μ„ 톡해 μ™„μ „νƒμƒ‰ν•˜λ©΄ 닡을 ꡬ할 수 μžˆλ‹€.

λ‹€λ₯Έ λ°©λ²•μœΌλ‘œλŠ” 닡을 ꡬ할 수 μ—†μ„κΉŒ ν•œμ°Έ μƒκ°ν•΄λ΄€λŠ”λ° 정말 μ—†λ‹€!

그럼 λ˜μ „ κ°œμˆ˜κ°€ 1000개만 주어져도 1000! 개수의 μˆœμ—΄μ„ νƒμƒ‰ν•΄μ•Όν•˜λŠ”λ°..

더 효율적인 방법은 μ—†λŠ”κ±΄κ°€?


from itertools import permutations


def solution(k, dungeons):
    answer = float('-inf')

    # 1. λ°©λ¬Έ ν•  λ˜μ „ μˆœμ—΄ κ΅¬ν•˜κΈ°
    for permutations_dungeons in permutations(dungeons):
        cur_tired = k
        tmp_answer = 0
        for dungeon in permutations_dungeons:
            min_tired = dungeon[0]
            use_tired = dungeon[1]
            # 2. ν˜„μž¬ ν”Όλ‘œλ„κ°€ μ΅œμ†Œ μš”κ΅¬ ν”Όλ‘œλ„ 이상이라면 λ˜μ „ νƒν—˜
            if min_tired <= cur_tired:
                cur_tired -= use_tired
                tmp_answer += 1
        answer = max(tmp_answer, answer)

    return answer


if __name__ == "__main__":
    k = 80
    dungeons = [[80, 20], [50, 40], [30, 10]]

    print(solution(k, dungeons))



Categories:

Updated:

Leave a comment