ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/2294
package main
import (
"bufio"
"fmt"
"math"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n, k int
fmt.Fscanln(reader, &n, &k)
var coins = make([]int, n)
var dp = make([]int, k+1)
for i := 0; i < n; i++ {
fmt.Fscanln(reader, &coins[i])
}
for i := 1; i < k+1; i++ {
dp[i] = 100000
}
for i := 1; i < len(dp); i++ {
for j := 0; j < n; j++ {
coin := coins[j]
if i-coin >= 0 {
dp[i] = int(math.Min(float64(dp[i]), float64(dp[i-coin]+1)))
}
}
}
if dp[k] == 100000 {
fmt.Fprintln(writer, -1)
} else {
fmt.Fprintln(writer, dp[k])
}
}
๐จ Python3
# https://www.acmicpc.net/problem/2294
import sys
if __name__ == "__main__":
n, k = list(map(int, sys.stdin.readline().split()))
coins = []
for i in range(n):
coins.append(int(sys.stdin.readline()))
dp = [100000 for _ in range(k+1)]
dp[0] = 0
for i in range(1, len(dp)):
for j in range(n):
coin = coins[j]
if i-coin >= 0:
dp[i] = min(dp[i], dp[i-coin]+1)
if dp[k] == 100000:
print(-1)
else:
print(dp[k])
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 1182๋ฒ / ๋ถ๋ถ์์ด์ ํฉ [Go][Python3] (0) | 2021.03.21 |
---|---|
BOJ / 9465๋ฒ / ์คํฐ์ปค [Go][Python3] (0) | 2021.03.20 |
BOJ / 11052๋ฒ / ์นด๋ ๊ตฌ๋งคํ๊ธฐ [Go][Python3] (0) | 2021.03.18 |
BOJ / 1697๋ฒ / ์จ๋ฐ๊ผญ์ง [Go][Python3] (0) | 2021.03.17 |
BOJ / 7576๋ฒ / ํ ๋งํ [Go][Python3] (0) | 2021.03.16 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ์๋ฐ
- MongoDB
- ์ด๋ถํ์
- Golang
- ๋ฐฑ์ค
- ๋งฅ๋ถ
- ์๊ฐ๊ต์ฒด
- ํ
- ballet
- dp
- ํด์๋งต
- BFS
- java
- python3
- ์คํ
- ๋ชฝ๊ณ ๋๋น
- baekjoon
- AWS
- ๋ธ๋ฃจํธํฌ์ค
- ๋งฅ๋ถํ๋ก
- dfs
- Macbook pro 2012 mid 13
- Algorithm
- BOJ
- ๋ถํ ์ ๋ณต
- ํ๋ก์ด๋์์ฌ
- ๋ฐ๋
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- go
- ์๊ณ ๋ฆฌ์ฆ
- Total
- Today
- Yesterday