c++ - Speeding up a function with dynamic programming -
i have program
//h our n static int g=0; int fun(int h){ if(h<=0){ g++; return g; } return g+fun(h-1)+fun(h-4); }
is possible speed using dynamic programming?
i figured out function runs in o(2^n)
i supposed reduce running time dynamic programming, not understand concept.
just asking nudge in right direction.
while can't give answer actual question, intrigued altogether different, namely statement
return g+fun(h-1)+fun(n-4);
obviously, function has side effect of changing global static variable g
. not 100% sure whether return
statement's expression evaluates in defined fashion, or whether result might undefined.
it might nice exercise think order in function calls executed, , how affects g
, thereby function's result.
Comments
Post a Comment