CutRodRec solve the problem recursively: initial approach
(price []int, length int)
| 8 | |
| 9 | // CutRodRec solve the problem recursively: initial approach |
| 10 | func CutRodRec(price []int, length int) int { |
| 11 | if length == 0 { |
| 12 | return 0 |
| 13 | } |
| 14 | |
| 15 | q := -1 |
| 16 | for i := 1; i <= length; i++ { |
| 17 | q = Max(q, price[i]+CutRodRec(price, length-i)) |
| 18 | } |
| 19 | return q |
| 20 | } |
| 21 | |
| 22 | // CutRodDp solve the same problem using dynamic programming |
| 23 | func CutRodDp(price []int, length int) int { |