I am trying to come up with the solution for a problem analogous to the following:
Using dynamic programming, solve the problem as to find the optimal way of spending T units of time to study which will yield the highest total score.
Thanks in advance for any help :)
My attempt:
S[][] = 0
for i = 1:n
for j = 0:T
max = 0
for k = 0:j
Grade = G[i][j]+ S[i-1][T-k]
if Grade > max
max = Grade
end for
S[i][j] = max
end for
end for
Dynamic programming is an optimization approach that transforms a complex problem into a sequence of simpler problems; its essential characteristic is the multistage nature of the optimization procedure.
Dynamic Programming Technique is similar to divide-and-conquer technique. Both techniques solve a problem by breaking it down into several sub-problems that can be solved recursively.
Let S[i][j]
represent the best score you can achieve spending j
units of time on the first i
exams. You can calculate S[i][j]
by looking at S[i-1][k]
for each value of k
. For each element of S
, remember the value of k
from the previous row that gave the best result. The answer to what the best score for studying all exams in time T
is just S[n][T]
, and you can use the values of k
that you remembered to determine how much time to spend on each exam.
S[][] = 0
for j = 0:T
S[0][j] = M[0][j]
for i = 1:n
for j = 0:T
max = 0
for k = 0:j
# This is the score you could get by spending j time, spending
# k time on this exam and j-k time on the previous exams.
Grade = S[i-1][j-k] + M[i][k]
if Grade > max
max = Grade
end for
S[i][j] = max
end for
end for
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With