Improve the performance of the following code:


  • 0
    J
    var test = {
        f: function(n) {
            if (n === 0) {
                return 0;
            }
            if (n === 1) {
                return 1;
            }
            else {
                return f(n - 1) +
                    f(n - 2);
            }
        }
    };

  • 0

    It's fibonacci, we can use dynamic programming to improve the performance, if the f function will be called many many times and n is within a constant range, then use hashmap.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.