[Codewars] Memoized Log Cutting

Memoized Log Cutting

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
// Your new function as given to you by me, your boss.
int cut_log(const std::vector<int>& p, int n){
// Some array to store calculated values
std::vector<int> dp(n + 1);
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= n - i; j++) {
dp[j+i] = std::max(dp[j+i], dp[j] + p[i]);
}
}
return dp[n];
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/10/PS/Codewars/memoized-log-cutting/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.