we have n numbers in a set A
A = {a1, a2, a3 ... an}
and a number s.
I want to pick up some numbers from A, and sum of these numbers will equals to s or closest to s(but smaller than s).
input:
3
1 2 7
9
output:
2 7
input:
3
1 2 7
5
output
1 2
Can I solve this problem using dynamic programming?
Thanks:)
Optimizing Loading Problem
Moderator: Board moderators
Optimizing Loading Problem
"Learning without thought is useless;thought without learning is dangerous."
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
"Hold what you really know and tell what you do not know -this will lead to knowledge."-Confucius
-
- Experienced poster
- Posts: 161
- Joined: Tue Oct 25, 2005 8:38 pm
- Location: buet, dhaka, bangladesh
Code: Select all
DP[n][s] = s, if n = 0
= DP[n-1][s], if a[n] > s
= min(DP[n-1][s-a[n]], DP[n-1][s]), otherwise
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
----------------
the world is nothing but a good program, and we are all some instances of the program