I am trying to come up with the solution for a problem analogous to the following:
- Let M be a matrix of n rows and T columns.
- Let each row have positive non-decreasing values. (e.g. row = [1, 2, 30, 30, 35])
- Let M[i][j] correspond to the score obtained by spending j units of time on exam i.
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
Let
S[i][j]represent the best score you can achieve spendingjunits of time on the firstiexams. You can calculateS[i][j]by looking atS[i-1][k]for each value ofk. For each element ofS, remember the value ofkfrom the previous row that gave the best result. The answer to what the best score for studying all exams in timeTis justS[n][T], and you can use the values ofkthat you remembered to determine how much time to spend on each exam.