Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Overlapping Subproblems Property: Tabulation | Intro to Dynamic Programming
Dynamic Programming
course content

Conteúdo do Curso

Dynamic Programming

Dynamic Programming

1. Intro to Dynamic Programming
2. Problems
3. Solutions

Overlapping Subproblems Property: Tabulation

Tabulation

"First, solve all necessary subproblems, and then solve the main problem."

Such a principle is called the Bottom-Up approach. We start with trivial subproblems and move from the bottom to the answer. This principle also uses additional tables to store solutions.

Example

Let’s create an array dp to store the solutions. (dp can be a common name for data structure in a class of DP problems).

1234567891011121314
def fib(n): # Array declaration dp = [0]*(n+1) # Base case assignment dp[0] = 0 dp[1] = 1 # Calculating and storing the values for trivial cases for i in range(2 , n+1): dp[i] = dp[i-1] + dp[i-2] return dp[n]
copy

Since we know how to calculate the next element using the previous two elements, let's move from the pre-defined first two elements (base case) and figure out the solution for the 3rd sub-problem. After that, solve the 4th sub-problem using the 2nd and 3rd, and so on, until the last element.

Tarefa

Look at the following task code for the Fibonacci problem.

  1. Fix it to make the solution correct.
  2. Call the function for n = 16 and output the 16th Fibonacci number.

Tarefa

Look at the following task code for the Fibonacci problem.

  1. Fix it to make the solution correct.
  2. Call the function for n = 16 and output the 16th Fibonacci number.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 3
toggle bottom row

Overlapping Subproblems Property: Tabulation

Tabulation

"First, solve all necessary subproblems, and then solve the main problem."

Such a principle is called the Bottom-Up approach. We start with trivial subproblems and move from the bottom to the answer. This principle also uses additional tables to store solutions.

Example

Let’s create an array dp to store the solutions. (dp can be a common name for data structure in a class of DP problems).

1234567891011121314
def fib(n): # Array declaration dp = [0]*(n+1) # Base case assignment dp[0] = 0 dp[1] = 1 # Calculating and storing the values for trivial cases for i in range(2 , n+1): dp[i] = dp[i-1] + dp[i-2] return dp[n]
copy

Since we know how to calculate the next element using the previous two elements, let's move from the pre-defined first two elements (base case) and figure out the solution for the 3rd sub-problem. After that, solve the 4th sub-problem using the 2nd and 3rd, and so on, until the last element.

Tarefa

Look at the following task code for the Fibonacci problem.

  1. Fix it to make the solution correct.
  2. Call the function for n = 16 and output the 16th Fibonacci number.

Tarefa

Look at the following task code for the Fibonacci problem.

  1. Fix it to make the solution correct.
  2. Call the function for n = 16 and output the 16th Fibonacci number.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 1. Capítulo 3
toggle bottom row

Overlapping Subproblems Property: Tabulation

Tabulation

"First, solve all necessary subproblems, and then solve the main problem."

Such a principle is called the Bottom-Up approach. We start with trivial subproblems and move from the bottom to the answer. This principle also uses additional tables to store solutions.

Example

Let’s create an array dp to store the solutions. (dp can be a common name for data structure in a class of DP problems).

1234567891011121314
def fib(n): # Array declaration dp = [0]*(n+1) # Base case assignment dp[0] = 0 dp[1] = 1 # Calculating and storing the values for trivial cases for i in range(2 , n+1): dp[i] = dp[i-1] + dp[i-2] return dp[n]
copy

Since we know how to calculate the next element using the previous two elements, let's move from the pre-defined first two elements (base case) and figure out the solution for the 3rd sub-problem. After that, solve the 4th sub-problem using the 2nd and 3rd, and so on, until the last element.

Tarefa

Look at the following task code for the Fibonacci problem.

  1. Fix it to make the solution correct.
  2. Call the function for n = 16 and output the 16th Fibonacci number.

Tarefa

Look at the following task code for the Fibonacci problem.

  1. Fix it to make the solution correct.
  2. Call the function for n = 16 and output the 16th Fibonacci number.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Tabulation

"First, solve all necessary subproblems, and then solve the main problem."

Such a principle is called the Bottom-Up approach. We start with trivial subproblems and move from the bottom to the answer. This principle also uses additional tables to store solutions.

Example

Let’s create an array dp to store the solutions. (dp can be a common name for data structure in a class of DP problems).

1234567891011121314
def fib(n): # Array declaration dp = [0]*(n+1) # Base case assignment dp[0] = 0 dp[1] = 1 # Calculating and storing the values for trivial cases for i in range(2 , n+1): dp[i] = dp[i-1] + dp[i-2] return dp[n]
copy

Since we know how to calculate the next element using the previous two elements, let's move from the pre-defined first two elements (base case) and figure out the solution for the 3rd sub-problem. After that, solve the 4th sub-problem using the 2nd and 3rd, and so on, until the last element.

Tarefa

Look at the following task code for the Fibonacci problem.

  1. Fix it to make the solution correct.
  2. Call the function for n = 16 and output the 16th Fibonacci number.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 1. Capítulo 3
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt