-
Notifications
You must be signed in to change notification settings - Fork 1
11-dohyeondol1 #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
11-dohyeondol1 #39
Conversation
|
μ€λͺ
μ μ ν΄μ£Όμ
μ, μ€λͺ
λ§ λ³΄κ³ λ¬Έμ λ₯Ό ν μ μμ μ λμ
λλ€ γ
γ
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
coins = [int(input()) for _ in range(n)]
dp = [0 for _ in range(k+1)]
dp[0] = 1
for coin in coins:
for j in range(coin, k + 1):
dp[j] += dp[j - coin]
print(dp[k]) |
hadongun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μΌ..μ΄ μμ§ dpλ¬Έμ μ μ΅μνμ§ μμμ μ΄κ²μ κ² μ¨λ³΄λ©΄μ ν΄λ μ λͺ¨λ₯΄κ² λ€μ©
μ€λͺ
μμΈν μ¨ μ£Όμ
μ μ€κ°μ ννΈλ₯Ό μ»κ³ κΎΈμκΎΈμ νμ΄μ¬μΌλ‘ νμ΄λμ΅λλ΅..!
dp λ³Όμλ‘ λ§€λ ₯μ μ΄κ΅°μ. μ‘°κΈ λ μ΅μν΄μ Έλ³΄κ² μ΅λλ€
λμ 1
n, k = map(int, input().split())
coin = []
for _ in range(n):
coin.append(int(input()))
dp = [0] * (k + 1)
dp[0] = 1
for i in range(n):
for j in range(coin[i], k + 1):
dp[j] = dp[j] + dp[j - coin[i]]
print(dp[k])There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νλ‘ μ£Όμλ μ΄ν΄νκΈ° νΈνμ΅λλ€! κ²½μ°μ μλ₯Ό λλλκ² 1,2,3 λ§λ€κΈ° λ¬Έμ λ λΉμ·ν λλμΈ κ±° κ°μμ. μ΄μ dpμ μ’ μ΅μν΄μ§λ κ±° κ°μ΅λλ€
λμ 1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
int main()
{
int n, k;
scanf("%d %d", &n, &k);
int* coin = (int*)malloc(sizeof(int) * n);
int* dp = (int*)malloc(sizeof(int) * (k + 1));
for (int i = 0; i < n; i++)
{
scanf("%d", &coin[i]);
}
memset(dp, 0, sizeof(int) * (k + 1));
dp[0] = 1;
for (int i = 0; i < n; i++)
{
for (int j = coin[i]; j < k + 1; j++)
{
dp[j] = dp[j] + dp[j - coin[i]];
}
}
printf("%d", dp[k]);
return 0;
}
π λ¬Έμ λ§ν¬
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
μ λ² λμ 0 μ΄ ν©μ΄ kκ° λλ λμ κ°μμ μ΅μκ°μ΄μλ€λ©΄,
μ΄λ² λ¬Έμ λ ν©μ΄ kκ° λλ λμ κ°μμ κ²½μ°μ μλ₯Ό ꡬν΄μΌ ν©λλ€.
μ€λͺ νκΈ°μ μμ,
ꡬνκ³ μ νλ κ°μΉ ν©μ΄ 0μΈ κ²½μ°, μ무 λμ λ μ ννμ§ μλ κ²½μ° νλκ° μ‘΄μ¬ν©λλ€.
λ°λΌμ 0μ νμ 1μ κ°μ κ°μ§λλ€. μ λ μ΄ λΆλΆμ μκ° λͺ»ν΄μ μκ°μ΄ λ κ±Έλ Έμ΅λλ€ γ
λ¬Έμ μ μμ μ λ ₯ 1κ³Ό κ°μ΄, λμ μ κ°μΉλ₯Ό 1, 2, 5λ‘ μ€μ νκ³ λ¬Έμ λ₯Ό λ°λΌλ΄ μλ€.
νΈμλ₯Ό μν΄μ λμ μ κ°μΉλ₯Ό μ(KRW, β©)μΌλ‘ μ€λͺ νκ² μ΅λλ€.
λ€μκ³Ό κ°μ νμμ μμν΄λ΄ μλ€.
λμ μ κ°μΉκ° μ£Όμ΄μ§μ§ μμ κ²½μ° 0λΆν° 10κΉμ§μ κ°μΉν©μ ννν μ μλ κ²½μ°μ μ ν μ΄λΈμ λλ€.
μ΄λ₯Ό κ°μΉ iμ λ°λ₯Έ dp[i]λΌκ³ μ€μ ν΄λκ² μ΅λλ€.
λ¨Όμ 1μλ§ μλ κ²½μ° 0λΆν° 10κΉμ§μ κ°μΉν©μ ννν μ μλ κ²½μ°μ μλ λ€μκ³Ό κ°μ΅λλ€.
κ·Έλ¦¬κ³ 2μλ§ μλ κ²½μ°λ λ€μκ³Ό κ°μ΅λλ€.
1μκ³Ό 2μμ΄ λ λ€ μλ κ²½μ°λ λ€μκ³Ό κ°μ΅λλ€.
μκ° μ΄λ€ κ·μΉμ±μ΄ μλκ²λ§ κ°μ£ ..?
κ΅³μ΄ κ΅³μ΄ μ¬κΈ°μ κ·μΉμ±μ μ°Ύμ보면, λ€μκ³Ό κ°μ΅λλ€.
1μκ³Ό 2μμ΄ λ λ€ μλ κ²½μ°λ κ·μΉμ±μ΄ μλ κ² κ°μ§λ§,
λ 립μ μΌλ‘ λ°λΌλ³΄λ©΄ μμ ꡬν κ·μΉμ±κ³Ό μ ν λ€λ₯Έ μμ΄ λμ€κ² λ©λλ€.
κ²°κ΅ λ¬Έμ μμ λμ μ κ°μΉκ° μ£Όμ΄μ§κ³ μ΄λ₯Ό μ΄μ©ν΄ κ·μΉμ±μ΄ μκ²¨μΌ νκΈ° λλ¬Έμ,
$\text{dp[i]} = \text{dp[i]}+\text{dp[i-1]}$ μ΄ μ νλκ³ ,
$\text{dp[i]} = \text{dp[i]}+\text{dp[i-2]}$ μ΄ κ³μ°λμ΄μΌ ν©λλ€.
1μκ³Ό 2μμ΄ λμμ μλ κ²½μ°λ
μ΄λ λμ μ κ°μΉλ₯Ό coin[j]λΌκ³ νμ λ, λ€μκ³Ό κ°μ΄ ννν μ μμ΅λλ€.
μ΄λ₯Ό κ²μ¦νκΈ° μν΄μ, μμ§ μ¬μ©νμ§ μμ 5μμ§λ¦¬ λμ μ μΆκ°ν΄λ΄ μλ€.
5μλ§ μλ κ²½μ° 0λΆν° 10κΉμ§μ κ°μΉν©μ ννν μ μλ κ²½μ°μ μλ λ€μκ³Ό κ°μ΅λλ€.
μ΄μ 1μ, 2μ, 5μμ΄ λͺ¨λ μλ κ²½μ°μ μλ₯Ό μμμ ꡬν κ·μΉμ±μ ν΅ν΄ νλνλ μμ ꡬν΄λ³΄λ©΄, λ€μκ³Ό κ°μ΅λλ€.
κ°μ ν κ·μΉμ±μ μ μ©ν΄ 1,2,5μμ λμ μ΄ μλ κ²½μ° κ°μΉν© 0λΆν° 10κΉμ§μ κ²½μ°μ μλ₯Ό κ³μ°ν κ³Όμ
λμ μ κ°μΉ = 1
$\text{dp}[1] \quad \text{+=} \quad \text{dp}[0] = 1 $
$\text{dp}[2] \quad \text{+=} \quad \text{dp}[1] = 1 $
$\text{dp}[3] \quad \text{+=} \quad \text{dp}[2] = 1 $
$\text{dp}[4] \quad \text{+=} \quad \text{dp}[3] = 1 $
$\text{dp}[5] \quad \text{+=} \quad \text{dp}[4] = 1 $
$\text{dp}[6] \quad \text{+=} \quad \text{dp}[5] = 1 $
$\text{dp}[7] \quad \text{+=} \quad \text{dp}[6] = 1 $
$\text{dp}[8] \quad \text{+=} \quad \text{dp}[7] = 1 $
$\text{dp}[9] \quad \text{+=} \quad \text{dp}[8] = 1 $
$\text{dp}[10] \quad \text{+=} \quad \text{dp}[9] = 1 $
λμ μ κ°μΉ = 2
$\text{dp}[2] \quad \text{+=} \quad \text{dp}[0] = 2 $
$\text{dp}[3] \quad \text{+=} \quad \text{dp}[1] = 2 $
$\text{dp}[4] \quad \text{+=} \quad \text{dp}[2] = 3 $
$\text{dp}[5] \quad \text{+=} \quad \text{dp}[3] = 3 $
$\text{dp}[6] \quad \text{+=} \quad \text{dp}[4] = 4 $
$\text{dp}[7] \quad \text{+=} \quad \text{dp}[5] = 4 $
$\text{dp}[8] \quad \text{+=} \quad \text{dp}[6] = 5 $
$\text{dp}[9] \quad \text{+=} \quad \text{dp}[7] = 5 $
$\text{dp}[10] \quad \text{+=} \quad \text{dp}[8] = 6 $
λμ μ κ°μΉ = 5
$\text{dp}[5] \quad \text{+=} \quad \text{dp}[0] = 4 $
$\text{dp}[6] \quad \text{+=} \quad \text{dp}[1] = 5 $
$\text{dp}[7] \quad \text{+=} \quad \text{dp}[2] = 6 $
$\text{dp}[8] \quad \text{+=} \quad \text{dp}[3] = 7 $
$\text{dp}[9] \quad \text{+=} \quad \text{dp}[4] = 8 $
$\text{dp}[10] \quad \text{+=} \quad \text{dp}[5] = 10 $
λ°λΌμ λ€μκ³Ό κ°μ κ²°κ³Όλ₯Ό ν μ΄λΈλ‘ ννν μ μμλλ·...
μ΄λ μ€μ 0λΆν° 10κΉμ§μ κ°μΉν©μ νννλ κ²½μ°μ μμ λμΌνλ©°,
μ΄ μ νμμ μ΄μ©ν νμ΄λ‘ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμ΅λλ€.
μλ μ½λ
π μλ‘κ² μκ²λ λ΄μ©