From the course: Fundamentals of Dynamic Programming

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Solving the flowerbox problem in Python

Solving the flowerbox problem in Python - Python Tutorial

From the course: Fundamentals of Dynamic Programming

Solving the flowerbox problem in Python

- [Instructor] To solve the flowerbox problem, we started by defining a function f of i. This function captured the maximum total height of the flowers we can achieve. If we only consider positions zero to i. We also defined the recurrence relation and how to extract the final answer from the recurrence relation. Finally, we drew out the dependency graph for this problem and noted it looked just like the one for the Fibonacci sequence. Let's see how all this translates into Python. We'll define a function flowerbox that takes in an array of nutrient values. We'll a have two variables a and b, to store the results from the last two subproblems. These are initialized to the values of the base cases f of zero and f of one. Now we work our way upto f of n minus one. Combining the results from the last two subproblems. And we discard the result of any subproblem we no longer need. Overall, this code looks very similar to the…

Contents