Dynamic Programming, Part 2–1

Flowerbox problem

Goal: Maximize the total height of all planted flowers.

Example solution

Maximized total height, 12
Maximized total height, 18
f(n-1) because of 0 index start
solution in python
#include <iostream>
#include <algorithm>
using namespace std;
int bed[5] = {3, 10, 3, 1, 2};
int n = 0;
int flowerbed(int n)
{
int a = 0;
int b = 0;
int temp;
for (int i=0; i<=n; i++) {
temp = a;
a = b;
b = max(temp + bed[i], b);
}
return b;
}
int main()
{
cin >> n;
cout << flowerbed(n-1) << endl;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store