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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Planting flowers in a flowerbox1m 56s
-
(Locked)
Breaking down the flowerbox problem into subproblems3m 27s
-
(Locked)
Solving the flowerbox problem in Python1m 19s
-
(Locked)
How many ways can you make change?1m 52s
-
(Locked)
Breaking down the change-making problem into subproblems4m 23s
-
(Locked)
Solving the change-making problem in Python2m 13s
-
-
-
-