Grokking Dynamic Programming Patterns for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
recursive, dynamic programming solution question

camelBack

Jan 18, 2024

Why is there a "+1" to the capacity when generating the 2d matrix? (the recursive, dynamic programming solution)

dp = [[-1 for x in range(capacity+1)] for y in range(len(profits))]

0

0

Comments
Comments
Stanimir Valchev
Stanimir Valcheva year ago

Because we want to have a storage for capacity number, not it's index. It makes for a cleaner solution, as we don't have to imagine every time that the last capacity is actually len(capacity) - 1. Makes an awkward code solution as we will have to do index - 1 all over t...

On this page

Problem Statement

Try it yourself