๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 17626 - Four Squares

by HandHand 2021. 3. 18.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 17626๋ฒˆ

ํ’€์ด ๊ณผ์ •

ํŠน์ • ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์ œ๊ณฑ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๋งŒ์•ฝ N ์˜ ์ œ๊ณฑ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ตœ๋Œ€ N**0.5 ๊นŒ์ง€๋งŒ ๊ฒ€์‚ฌํ•ด๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๋•Œ๋ฌธ์— ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์™„์ „ ํƒ์ƒ‰ ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์„ ์ˆ˜ ์žˆ์ง€๋งŒ ์ด ๊ฒฝ์šฐ ์ค‘๋ณต ๊ฒ€์‚ฌ๋กœ ์ธํ•ด ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ํ”ผํ•˜์ง€ ๋ชปํ•˜๋ฉฐ
๋”ฐ๋ผ์„œ ์ด๋ฅผ ์ตœ์ ํ™”ํ•˜๊ธฐ ์œ„ํ•ด ๋™์  ๊ณ„ํš๋ฒ• ์„ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

dp(n): n์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•œ ์ตœ์†Œ ์ œ๊ณฑ์ˆ˜์˜ ๊ฐœ์ˆ˜ ๋ผ๊ณ  ์ •์˜ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ ํ™”์‹์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


do(n) = min(dp(n - k^2), dp(n - (k - 1)^2), ... dp(n - 1)) + 1

์ฝ”๋“œ


import sys

N = int(input())


def solution():
    dp = [-1] * (N + 1)

    dp[0] = 0
    dp[1] = 1

    for n in range(2, N + 1):
        dp[n] = sys.maxsize
        for k in range(1, int(n ** 0.5) + 1):
            dp[n] = min(dp[n], dp[n - k**2] + 1)

    return dp[N]


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 1963 - ์†Œ์ˆ˜ ๊ฒฝ๋กœ  (0) 2021.03.18
BOJ 4195 - ์นœ๊ตฌ ๋„คํŠธ์›Œํฌ  (0) 2021.03.18
BOJ 1303 - ์ „์Ÿ - ์ „ํˆฌ  (0) 2021.03.18
BOJ 1260 - DFS์™€ BFS  (0) 2021.03.18
BOJ 6550 - ๋ถ€๋ถ„ ๋ฌธ์ž์—ด  (0) 2021.03.18

๐Ÿ’ฌ ๋Œ“๊ธ€