MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / CutRodDp

Function CutRodDp

dynamic/rodcutting.go:23–36  ·  view source on GitHub ↗

CutRodDp solve the same problem using dynamic programming

(price []int, length int)

Source from the content-addressed store, hash-verified

21
22// CutRodDp solve the same problem using dynamic programming
23func CutRodDp(price []int, length int) int {
24 r := make([]int, length+1) // a.k.a the memoization array
25 r[0] = 0 // cost of 0 length rod is 0
26
27 for j := 1; j <= length; j++ { // for each length (subproblem)
28 q := -1
29 for i := 1; i <= j; i++ {
30 q = Max(q, price[i]+r[j-i]) // avoiding recursive call
31 }
32 r[j] = q
33 }
34
35 return r[length]
36}
37
38/*
39func main() {

Callers

nothing calls this directly

Calls 1

MaxFunction · 0.85

Tested by

no test coverage detected