Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/dynamic_prog/maxprod_memo by daveagp@gmail.com. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Define a memoized recursive function to compute <i>maxprod(N)</i>.
Public permissions remove
Engine
Template / Reference solution
 
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
double maxprod(int n, vector<double>& memory) {
   // check if answer was already computed
   if (\[memory[n] != 0]\)
      return \[memory[n]]\;
   // else save answer 
   if (\[n <= 3]\)
      memory[n] = \[n]\;
   else
      memory[n] = \[max(2*maxprod(n-2, memory), 3*maxprod(n-3, memory))]\;
   return memory[n];
}
int main(int argc, char* argv[]) {
   int N = atoi(argv[1]);
   vector<double> memory(N+1, \[0]\); // sets initial value for vector
   cout << setprecision(20) << maxprod(N, memory) << endl;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {"args": ["6"]},
   {"args": ["7"]},
   {"args": ["8"]},
   {"args": ["20"]},
   {"args": ["60"]},
   {"args": ["100"]},
   {"args": ["101"]},
   {"args": ["102"]},
   {"args": ["500"]},
   {"args": ["1812"]}
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.