Algorithms
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ฉ๋ฆฌ๋ฐ๊ธฐ - Python
thals0
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