Skip to content

Latest commit

 

History

History
33 lines (24 loc) · 545 Bytes

Dynamic Programming.MD

File metadata and controls

33 lines (24 loc) · 545 Bytes

Dynamic Programming

Basic Concept

  • Recursive solution: repeated calls for same inputs --> optimize using Dynamic Programming by storing the results of subproblems.

  • Fibonacci Example

    • Recursive Solution
    int fib(int n) {
      if (n <= 1)
        return 1;
       return fib(n-1) + fib(n-2);
    }
    • Dynamic Programming Solution
    int fib(int n) {
      int f[n + 1];
      f[0] = 1;
      f[1] = 1;
      
      for (int i = 0; i <= n; i++) {
        f[i] = f[i-1] + f[i-2];
      }
      
      return f[n];
    }