ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

2294๋ฒˆ: ๋™์ „ 2

์ฒซ์งธ ์ค„์— n, k๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 100, 1 ≤ k ≤ 10,000) ๋‹ค์Œ n๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ๊ฐ์˜ ๋™์ „์˜ ๊ฐ€์น˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋™์ „์˜ ๊ฐ€์น˜๋Š” 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๊ฐ€์น˜๊ฐ€ ๊ฐ™์€ ๋™์ „์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ฃผ

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ 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
๋Œ“๊ธ€