ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/17626
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
fmt.Fprintln(writer, getSquareCount(n))
}
func getSquareCount(n int) int {
dp := make([]int, n+1)
dp[1] = 1
for i := 2; i < n+1; i++ {
minCount := n
for j := 1; j*j <= i; j++ { // i๋ณด๋ค ์์ ์ ๊ณฑ์๋ค์ dp๊ฐ ์ค ์ต์๊ฐ์ ๊ตฌํ๋ค
if minCount > dp[i-j*j] {
minCount = dp[i-j*j]
}
}
dp[i] = minCount + 1 // ์ต์๊ฐ๊ณผ ํด๋น ์ ๊ณฑ์๋ฅผ ๋ํ๋ ๊ฒฝ์ฐ๋ก 1์ ๋ํ ๊ฐ์ dp[i]์ ์ ์ฅํ๋ค
}
return dp[n]
}
๐จ Python3
# https://www.acmicpc.net/problem/17626
import sys
def get_square_count(n):
min_count = 4
for i in range(int(n**0.5), 0, -1):
if i*i == n:
min_count = min(min_count, 1)
break
else:
new_n = n - i*i
for j in range(int(new_n**0.5), 0, -1):
if new_n == j*j:
min_count = min(min_count, 2)
break
else:
new_n2 = n - i*i - j*j
for k in range(int(new_n2**0.5), 0, -1):
if new_n2 == k*k:
min_count = min(min_count, 3)
return min_count
if __name__ == "__main__":
n = int(sys.stdin.readline())
print(get_square_count(n))
# ์๊ฐ ์ด๊ณผ ๋ฐ์
# def get_square_count(n):
# dp = [0 for i in range(n+1)]
# dp[1] = 1
# for i in range(2, n+1):
# min_count = n
# j = 1
# while j*j <= i:
# if min_count > dp[i-j*j]:
# min_count = dp[i-j*j]
# j += 1
# dp[i] = min_count + 1
# return dp[n]
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1969๋ฒ / DNA [Go][Python3] (0) | 2021.01.24 |
---|---|
BOJ / 2503๋ฒ / ์ซ์ ์ผ๊ตฌ [Go][Python3] (0) | 2021.01.23 |
BOJ / 19947๋ฒ / ํฌ์์ ๊ท์ฌ ๋ฐฐ์ฃผํ [Go][Python3] (0) | 2021.01.20 |
BOJ / 9655๋ฒ / ๋ ๊ฒ์ [Go][Python3] (0) | 2021.01.19 |
BOJ / 13301๋ฒ / ํ์ผ ์ฅ์๋ฌผ [Go][Python3] (0) | 2021.01.18 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ํ
- Golang
- ์๋ฐ
- ๋งฅ๋ถํ๋ก
- MongoDB
- ์คํ
- BFS
- ๋ฐฑ์ค
- python3
- Algorithm
- ๋ชฝ๊ณ ๋๋น
- ์ด๋ถํ์
- ๋ฐ๋
- ballet
- dfs
- ์๊ฐ๊ต์ฒด
- ๋ธ๋ฃจํธํฌ์ค
- java
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ์๊ณ ๋ฆฌ์ฆ
- ํ๋ก์ด๋์์ฌ
- ๋ถํ ์ ๋ณต
- baekjoon
- ํด์๋งต
- AWS
- Macbook pro 2012 mid 13
- BOJ
- dp
- ๋งฅ๋ถ
- go
- Total
- Today
- Yesterday