๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ๋™์ „2 BOJ #2294

by seolhee2750 2022. 8. 31.
๋ฌธ์ œ

https://www.acmicpc.net/problem/2294

 

2294๋ฒˆ: ๋™์ „ 2

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

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class BOJ_2294 {

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		st = new StringTokenizer(in.readLine());
		int n = Integer.parseInt(st.nextToken());
		int k = Integer.parseInt(st.nextToken());
		int[] coin = new int[n];
		for(int i = 0; i < n; i++) {
			coin[i] = Integer.parseInt(in.readLine());
		}

		int[] memory = new int[k+1];
		Arrays.fill(memory, 100001);
		memory[0] = 0;
		for(int c: coin) {
			for(int i = c; i < k+1; i++) {
				memory[i] = Math.min(memory[i], memory[i - c] + 1);
			}
		}

		System.out.println(memory[k] == 100001 ? -1 : memory[k]);
	}
}

๐Ÿ‘‰ DP ๋ฌธ์ œ

์ ํ™”์‹ : memory[i] = Math.min(memory[i], memory[i - c] + 1);

 

โœ”๏ธ ๋ฐ˜๋ก€ ์ถ”์ฒœ
2 100
7
8

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • DP๋Š” ํ•ญ์ƒ ์ ํ™”์‹ ๊ตฌํ•˜๋Š”๊ฒŒ ํž˜๋“ ๋ฐ..!! ์ด๋ฒˆ์—๋„ ์ฒ˜์Œ์— ํ—›๋‹ค๋ฆฌ ์งš์–ด์„œ ์—„์ฒญ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.ใ…Ž

 

๋Œ“๊ธ€