๐ŸŒฑ → ๐ŸŒณ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฉ€๋ฆฌ๋›ฐ๊ธฐ - Python ๋ณธ๋ฌธ

Algorithms

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฉ€๋ฆฌ๋›ฐ๊ธฐ - Python

BAY 2023. 8. 14. 09:54
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/12914

 

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

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

์ฝ”๋“œ

์‹œ๊ฐ„์ดˆ๊ณผ ๋ฐœ์ƒ

from itertools import combinations
def solution(n):
    answer = 1
    b = 1
    if n % 2 == 0 : 
        while n > 2*b:
            answer += len(list(combinations(range(n-b), b)))
            b += 1
        answer += 1
    else:
        while n >= 2*b:
            answer += len(list(combinations(range(n-b), b)))
            b += 1
            if n < 2*b: break
    return answer

 

์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ ํ’€์ด (ํ”ผ๋ณด๋‚˜์น˜)

import sys
sys.setrecursionlimit(10**7)

memo=[1,1]

def solution(n):
    if len(memo)-1>=n:
        return memo[n]
    else:
        memo.append(solution(n-1)+solution(n-2))

    return memo[n]%1234567

 

ํ”ผ๋ณด๋‚˜์น˜

def solution(n):
    a, b = 1, 2
    for i in range(2,n):
        a, b = b, a+b
    return b

 

๋ฌธ์ œ๋ณด๋ฉด ์กฐํ•ฉ๋ฐ–์— ์ƒ๊ฐ์•ˆ๋‚˜๋˜๋ฐ ํ”ผ๋ณด๋‚˜์น˜๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•ด๋‚ด์‹ ๊ฑฐ์ง€ 

..

๊ฐˆ๊ธธ์ด ๋ฉ€๋‹ค ๋ฉ€์–ด ..!>!>

728x90