๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿงฉ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์Šคํ„ฐ๋”” ํšŒ๊ณ 

by ye-ha 2021. 11. 11.

(์‚ฌ์ง„: ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋งˆ์Šค์ฝ”ํŠธ ๋จธ์“ฑ์ด)

2021๋…„ 7-8์›”์— ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ์ง„ํ–‰ํ•˜๋Š” ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์™€ ์‹ค๋ฌด ์—ญ๋Ÿ‰ ๋ชจ๋‘ ์žก๋Š” ์Šคํ„ฐ๋””: Python๋ฐ˜์— ์ฐธ์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹น์‹œ ์Šคํ„ฐ๋””๊ฐ€ ๋๋‚œ ํ›„ ์ผ๋˜ ์ฃผ์ฐจ๋ณ„ ํšŒ๊ณ ๋ฅผ ์—ฎ์–ด์„œ ์ˆ˜์ • ํ›„ ์žฌ๋ฐœํ–‰ํ•˜๋Š” ๊ธ€์ž…๋‹ˆ๋‹ค.

 

์Šคํ„ฐ๋”” ์ค‘

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋ฌธ์ œ ํ’€์ด๋ฅผ ์—ด์‹ฌํžˆ ํ–ˆ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๊ตฌ๊ฐ„์— ๋‹ค๋‹ค๋ฅด๋‹ˆ ์‹ค๋ ฅ์˜ ์ •์ฒด๋ฅผ ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค. ์ฑ…์„ ํ†ตํ•ด ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ์ฑ…์˜ ํ•œ๊ณ„๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ž„ํŒฉํŠธ ์บ ํผ์Šค๋ฅผ ํ†ตํ•ด ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ์ง„ํ–‰ํ•˜๋Š” ํŒŒ์ด์ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์Šคํ„ฐ๋””์— ์ฐธ์—ฌํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ˜ผ์ž ๊ณต๋ถ€ํ•  ๋•Œ๋Š” ํ’€ ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๋Š” ํ’€ ์ˆ˜ ์—†๋‹ค๋Š” (?) ํ•œ๊ณ„๊ฐ€ ์žˆ์—ˆ๊ณ , ์ฑ…์œผ๋กœ ๋ฐฐ์šฐ๋ฉฐ ์ฑ… ์† ๊น”๋”ํ•œ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ์น˜๋‹ค ๋ณด๋‹ˆ ๋‚ด ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ์„ ๋Š๋ผ์ง€ ๋ชปํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค...

ํ•˜์ง€๋งŒ ์Šคํ„ฐ๋””๋ฅผ ์‹œ์ž‘ํ•˜๋ฉฐ ์ฝ”๋“œ์— ๋งŽ์€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค..๐Ÿ˜ญ ํŒŒ์ด์จ๋‹‰ํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ์€ ํŒŒ์ด์ฌ์ด ์ฒ˜์Œ์ด๋ผ ๊ทธ๋ ‡๋‹ค๊ณ  ์ณ๋„, ๋กœ์ง์ด ๋น„ํšจ์œจ์ ์ด๊ฑฐ๋‚˜ ์ง€์ €๋ถ„ํ•œ ๊ฒƒ์€ ๋„ˆ๋ฌด ๋ถ€๋„๋Ÿฝ๋”๋ผ๊ณ ์š”. ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์€ ์ดํ•ดํ•˜๊ณ  ์•”๊ธฐํ–ˆ๋Š”๋ฐ, ์ฝ”๋“œ๊ฐ€ ์ง€์ €๋ถ„ํ•œ ๊ฒƒ์€ ์‰ฝ๊ฒŒ ๊ณ ์ณ์ง€์ง€ ์•Š์•„ 4์ฃผ ๋‚ด๋‚ด ์ด ๋ถ€๋ถ„์„ ์‹ ๊ฒฝ์ผ์Šต๋‹ˆ๋‹ค.

 

์Šคํ„ฐ๋”” ๊ทธ ํ›„ 

์Šคํ„ฐ๋””๋ฅผ ๋งˆ์น˜๊ณ  ๋‚˜๋ฉด ์ด์ œ ๋‹ค์‹œ ์‹œ์ž‘์ด๋ผ๋Š” ๊ธฐ๋ถ„์ด ๋“ญ๋‹ˆ๋‹ค. ๋ฐฐ์šด ๋ฐฉ๋ฒ•์„ ํ† ๋Œ€๋กœ ๋” ๋งŽ์ด ์—ฐ์Šตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.. ์ €๋Š” ์Šคํ„ฐ๋””ํ•˜๋ฉฐ ๋งŒ๋‚œ ์—ด์ • ์žˆ๋Š” ๋™๋ฃŒ๋“ค๊ณผ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์Šคํ„ฐ๋””๋ฅผ ์ด์–ด๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ์•„์‰ฝ๊ฒŒ๋„ ๋‹ค๋ฅธ ์–ธ์–ด๋ฅผ ๊ณต๋ถ€ํ•˜๊ธฐ ์‹œ์ž‘ํ•ด ๊ธฐ์—… ์ฝ”ํ…Œ์— ์‘์‹œํ•˜๊ณ  ์žˆ์ง€๋Š” ๋ชปํ•˜์ง€๋งŒ, ๋ฌธ์ œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์„ ์•„์˜ˆ ์žŠ์–ด๋ฒ„๋ฆฌ์ง€ ์•Š์„ ์ •๋„๋กœ ๊ณ„์† ๊ณต๋ถ€ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

์Šคํ„ฐ๋””์˜ ์žฅ์ , ๊ทผ๋ฐ ์ด์ œ ๊ฐœ์ธ์ ์ธ ์†Œ๊ฐ์„ ๋ง๋ถ™์ธ

  • ๋ฆฌ๋”(ํ˜„์ง ๊ฐœ๋ฐœ์ž)๊ฐ€ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ํ•ด์ค€๋‹ค.
    -> ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์„ ์ตํžˆ๊ฑฐ๋‚˜ ์™ธ์›Œ์„œ ํ‘ผ ์‚ฌ๋žŒ(๋‚˜)์€ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋Š” ๋Œ€์ฒด๋กœ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€ ๊ฒƒ์ด๊ณ , ๋ชป ํ‘ธ๋Š” ๋ฌธ์ œ๋Š” ์•„์˜ˆ ์†๋„ ๋ชป๋Œˆ ๊ฒƒ.. ๊ทธ๋ž˜์„œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•๋ณด๋‹ค๋Š” ๋‹ค๋ฅธ ๋ถ€๋ถ„์—์„œ ๋ฆฌ๋ทฐ๋ฐ›๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์•˜์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋‚˜๋งŒ์˜ ์ƒ๊ฐ์œผ๋กœ ํ‘ธ์‹œ๋Š” ๋ถ„๋“ค์ด๋ผ๋ฉด ๋” ํฐ ๋„์›€์ด ๋  ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ๋ฆฌ๋”๊ฐ€ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ํ‘ธ๋Š” ๋…ธํ•˜์šฐ๋ฅผ ์ „์ˆ˜ํ•ด์ค€๋‹ค.
    -> ์ฒ˜์Œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋ฅผ ์ค€๋น„ํ•˜๋Š” ๋ถ„๋“ค / ๊ณต๋ถ€๋ฅผ ๊ฝค ํ•˜์…จ๋Š”๋ฐ ์‹ค๋ ฅ์˜ ์ •์ฒด๋ฅผ ๊ฒช๊ณ  ๊ณ„์‹  ๋ถ„๋“ค ๋ชจ๋‘์—๊ฒŒ ๋„์›€์ด ๋  ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ์Šคํ„ฐ๋””๊ฐ€ ๋๋‚˜๊ณ  ์Šคํ„ฐ๋”” ํŽ˜์ด์ง€๊ฐ€ ์‚ฌ๋ผ์ง€์ง€ ์•Š๋Š”๋‹ค
    -> ์ฃผ์ฐจ๋ณ„ ์„ธ์…˜, ๋ฌธ์ œ, ํŒ๋“ค์ด ๊ณ„์† ๋‚จ์•„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚œ์ด๋„ ๋“ฑ ์—ฌ๋Ÿฌ ์ด์œ ๋กœ ๋ชป ํ’€๊ณ  ๋†“์นœ ๋ถ€๋ถ„๋„ ์žˆ๋Š”๋ฐ, ๋‘๊ณ  ๋‘๊ณ  ๊ณต๋ถ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

 


1์ฃผ์ฐจ (Queue & Heap)

# ๋ฆฌ๋ทฐ ํ›„ 
if 0 <= nx < n and 0 <= ny < m and image[nx][ny] == now:
    image[nx][ny] = 0
    q.append((nx, ny))

# ๋ฆฌ๋ทฐ ์ „
if nx < 0 or nx >= n or ny < 0 or ny >= m:
    continue
if image[nx][ny] == 0:
    continue
if image[nx][ny] == now:
    image[nx][ny] = 0
    q.append((nx, ny))

๋ฆฌ๋ทฐ ์ „ ์ฝ”๋“œ๋Š” ์ฒ˜์Œ bfs๋ฅผ ๊ณต๋ถ€ํ•  ๋•Œ ์ฑ…์—์„œ ๋ดค๋˜ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
์ฒ˜์Œ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์•˜์„ ๋•Œ๋Š” ์กฐ๊ฑด์„ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์„ ํ˜ธ์˜ ์ฐจ์ด๊ฐ€ ์•„๋‹Œ๊ฐ€? ์ƒ๊ฐํ•˜๊ธด ํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ ๊ทธ ํ›„ ์ด์–ด์ง€๋Š” ๋ฆฌ๋ทฐ์—์„œ ์ œ๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๋งˆ์น˜ ๋‹จ๊ณ„๋ฅผ ๋ฐŸ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์กฐ๊ฑด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค .. ์ฒ˜์Œ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ „๊ฐœํ•˜๋Š” ๋ฐฉ์‹์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด๋ณด๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


# ๋ฆฌ๋ทฐ ํ›„
if day > prev:
    answer.append(count)
    count = 0
count += 1

# ๋ฆฌ๋ทฐ ์ „
if day == prev:
    count += 1
elif day > prev:
    answer.append(count)
    count = 1

์กฐ๊ฑด์„ ์ด๋Ÿฐ ์‹(?)์œผ๋กœ ์ฃผ๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ ์š”..


sum(ele ** 2 for ele in works) # ํšจ์œจ์„ฑ ๋” ์ข‹์Œ
sum([ele ** 2 for ele in works]) 

์œ„์˜ sum์ด ๋” ๋น ๋ฅด๊ฒŒ ๋‚˜์˜จ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•ด์„œ ์—ฌ์ญค๋ณด์•˜๋”๋‹ˆ ์‚ฌ๋žŒ์ด ์ธ์ง€ํ•  ์ˆ˜ ์—†์„ ์ •๋„์˜ ์ฐจ์ด๋กœ ๋น ๋ฅผ ์ˆ˜๋Š” ์žˆ์ง€๋งŒ ์‹ค์ œ ์‹คํ–‰ ์‹œ๊ฐ„์— ์˜ํ–ฅ์„ ์ค„ ์ •๋„๋Š” ์•„๋‹ˆ๋ผ๊ณ  ๋‹ต๋ณ€ ์ฃผ์…จ์Šต๋‹ˆ๋‹ค. ใ…Ž๊ถ๊ธˆํ•œ ๋ถ€๋ถ„๋„ ๋งˆ์Œ๊ป ์งˆ๋ฌธํ–ˆ์Šต๋‹ˆ๋‹ค.


# ๋ฆฌ๋ทฐ ํ›„
while scoville[0] < K:
    try:
        heappush(scoville, heappop(scoville) + heappop(scoville) * 2)
        answer += 1
    except:
        return -1

# ๋ฆฌ๋ทฐ ์ „
while scoville[0] < K:
    first = heapq.heappop(scoville)
    if scoville:
        new_scov = first + (heapq.heappop(scoville) * 2)
        heapq.heappush(scoville, new_scov)
        answer += 1
    else:
        return -1

ํŒŒ์ด์ฌ์˜ try-except๋ฌธ์„ ์•Œ๊ณ ๋งŒ ์žˆ์—ˆ๋Š”๋ฐ ์ฝ”๋“œ์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ฐธ๊ณ  ๋งํฌ๋„ ๊ณต์œ ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค.
heapq.heapreplace์˜ ์กด์žฌ๋„ ์•Œ๋ ค์ฃผ์…จ์Šต๋‹ˆ๋‹ค. ใ…Žใ…Ž


2์ฃผ์ฐจ (Stack & Hash)

p = re.compile('(\(\))?(\{\})?(\[\])?')
s = re.sub(p, '', s)

ํ•œ ํ…Œ์ŠคํŠธ์—์„œ ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ํšจ์œจ์„ฑ์—์„œ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ์—ฌ์ญค ๋ณด๋‹ˆ ์ •๊ทœํ‘œํ˜„์‹์€ ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š์•„ ์ฝ”ํ…Œ์—์„œ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค๋Š” ๋‹ต๋ณ€์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.


# ๋ฆฌ๋ทฐ ์ „
if not stack:
    return True
else:
    return False

# ๋ฆฌ๋ทฐ ํ›„
return not stack

if else ๊ตฌ๋ฌธ์„ ์•„์ฃผ ์ •์งํ•˜๊ฒŒ ์“ฐ๋Š” ํŽธ...


# ๋ฆฌ๋ทฐ ์ „
for name in names:
    weight = int(trucks[name])
    if weight == max_weight:
        sum_weight = 0
        answer += 2
        continue
    if weight < (max_weight - sum_weight):
        sum_weight += weight
    elif weight > (max_weight - sum_weight):
        sum_weight = weight
        answer += 1
    elif weight == (max_weight - sum_weight):
        sum_weight = 0
        answer += 1

# ๋ฆฌ๋ทฐ ํ›„
for name in names:
    weight = int(trucks[name])
    if weight > (max_weight - sum_weight):
        sum_weight = 0
        answer += 1
    sum_weight += weight

์ œ๊ฐ€ ๋‹จ๊ณ„๋งˆ๋‹ค ์กฐ๊ฑด์ฃผ๋Š” ๊ฒƒ์„ ์ด๋ ‡๊ฒŒ๋‚˜ ์ข‹์•„ํ•˜๋Š” ์‚ฌ๋žŒ์ž…๋‹ˆ๋‹ค..


# ๋ฆฌ๋ทฐ ์ „
if sum(board[i]) == 0:
    answer += 1
# ๋ฆฌ๋ทฐ ํ›„
answer += int(sum(board[i]) == 0)

์ด๋ ‡๊ฒŒ ํ•œ ์ค„๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


3์ฃผ์ฐจ (Searching)

3์ฃผ์ฐจ๋ถ€ํ„ฐ ๋ฌธ์ œ์˜ ๋‚œ์ด๋„๊ฐ€ ์ƒ์Šนํ–ˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ ํ’€์ดํ•˜๋ฉฐ ๋Š๋‚€ ๊ฒƒ์€ ํ˜ผ์ž ์–ด๋–ป๊ฒŒ๋“  ํ’€์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๋ฐ˜๋ฉด ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด๋ฉด ํ’€์ง€ ๋ชปํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. 3์ฃผ์ฐจ ์ฃผ์ œ๊ฐ€ Searching์ธ ๋งŒํผ ํ›„์ž์˜ ๋ฌธ์ œ๊ฐ€ ๋งŽ์•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ํ˜ผ์ž ๊ณต๋ถ€ํ•ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์ด ๋ณธ๊ฒฉ์ ์œผ๋กœ ๋งŽ์•„์ง„ ์ฃผ์ฐจ๋ผ๊ณ  ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค.

# ๋ฆฌ๋ทฐ ์ „
prime = set([i for i in range(2, n) if prime[i] == True])
# ๋ฆฌ๋ทฐ ํ›„
prime = set([i for i in range(2, n) if prime[i]])

์ทจํ–ฅ์— ๋”ฐ๋ผ ์•„๋ž˜์ฒ˜๋Ÿผ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ทจํ–ฅ๋Œ€๋กœ ์„ ํƒํ•œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์•„๋ž˜์ฒ˜๋Ÿผ ์“ธ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ๊ธฐ์— ๋„์›€๋˜์—ˆ์Šต๋‹ˆ๋‹ค.


for i in range(1, len(weights) + 1):
    # combi.append([sum(data) for data in itertools.combinations(weights, i)])
    answer += [sum(data)
                for data in itertools.combinations(weights, i)].count(m)
return answer

.count(number)์˜ ์กด์žฌ๋ฅผ ๋ชฐ๋ž์–ด์„œ ์ฃผ์„์— ์žˆ๋Š” ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•œ ํ›„ ์ฐพ์œผ๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋งŒ ํŠน์ •ํ•œ ๊ฐ’์„ ์ฐพ์•„์•ผํ•œ๋‹ค๋ฉด ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์“ธ ์ˆ˜ ์žˆ๋‹ค.


sieve = [True] * n

for i in range(2, int(n ** 0.5) + 1):
    if sieve[i] == True:
        for j in range(i + i, n, i):
            sieve[j] = False
prime = [i for i in range(2, n) if sieve[i] == True]

์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ๊ฐœ๋…๋„ ๊ณต๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค.


4์ฃผ์ฐจ (Sorting & Dynamic Programming)

DP๋Š” ๊ณต๋ถ€ํ•˜์ง€ ์•Š์•˜์—ˆ๊ธฐ์— 4์ฃผ์ฐจ ๋ฌธ์ œ๋Š” ์†Œํ™”ํ•˜๊ธฐ ๋ฐ”๋นด์Šต๋‹ˆ๋‹ค.

def solution(n):
    dp = [0 for i in range(n + 1)]
    dp[1] = 1
    dp[2] = 2
    for i in range(3, n + 1):
        dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007
    return dp[n]

DP์˜ ๊ธฐ๋ณธ ๋ฌธ์ œ๋ผ๋Š” 2xn ํƒ€์ผ๋ง ๋ฌธ์ œ ์ž…๋‹ˆ๋‹ค.
ํ•™๊ต๋‹ค๋‹ ๋•Œ๋„ ์ ํ™”์‹์ด ๊ทธ๋ ‡๊ฒŒ ์–ด๋ ค์› ์—ˆ๋Š”๋ฐ.. ๋‹ค์‹œ ๋งŒ๋‚œ๊ฑธ ๋ณด๊ณ  ๋งŒ๊ฐ์ด ๊ต์ฐจํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ ๋•Œ ํ•  ๊ฑธ

๋™๋นˆ๋‚˜๋‹˜์ด ์˜ฌ๋ ค์ค€ ์˜์ƒ์„ ๋ณด๊ณ  ๊ฐ์„ ์žก์•˜๊ณ , ๋™๋ฃŒ์˜ ๋„์›€์œผ๋กœ ์กฐ๊ธˆ ๋” ๊นŠ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.


๋งˆ์ง€๋ง‰ ์„ธ์…˜ ๋•Œ๋Š” ์งˆ์˜์‘๋‹ต๊ณผ ์†Œ๊ฐ์„ ๋‚˜๋ˆ„๋Š” ์‹œ๊ฐ„์ด ๋”ฐ๋กœ ์žˆ์Šต๋‹ˆ๋‹ค. ์ €๋Š” ์งˆ์˜์‘๋‹ต ์‹œ๊ฐ„๋„ ๋„์›€์ด ๋งŽ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํฌํŠธํด๋ฆฌ์˜ค๋‚˜ ์ด๋ ฅ์„œ๋ฅผ ๋ด์ค„์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ํ•˜์…จ์œผ๋‹ˆ ์ด ๋ถ€๋ถ„๋„ ์ž˜ ์ค€๋น„ํ•˜์…”์„œ ์ฐธ์„ํ•˜์‹œ๋ฉด ํฐ ๋„์›€์ด ๋  ๊ฒƒ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€