Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Problem B. Minimum path | Problems
Dynamic Programming
course content

Contenido del Curso

Dynamic Programming

Dynamic Programming

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

Problem B. Minimum path

The tasks in this section contain test function calls. Please do not change this code; otherwise, the assignment may not be accepted.

Given a two-dimensional array mat with values in it. Each value means the price we should pay for entering it. There is frog sitting in the top-left cell who wants to move to top-right. The frog can move to the nearest cell either right or down per one move. When it enters a cell, frog has to pay mat[i][j] for visiting it. Your goal is to find a path with minimal price. Return the cost of such a path.

Example 1

The orange path is minimum and costs 25.

Example 2

Input :

[[1, 3, 4],

[2, 1, 5],

[4, 6, 7]]

Output: 16

The path looks like:

Example 3

Input:

[[1, 2, 4],

[8, 5, 1]]

Output: 8

The Optimal Substructure here is to find the minimum path for each cell based on previous ones:

mat[i][j] = mat[i][j] + min(mat[i-1][j], mat[i][j-1])

This way, the minimum path to the mat[i][j] cell includes the price of this cell and the minimum price to one of the available cells (top or left).

Tarea

Create an algorithm to find the shortest path for the frog.

  1. Use data structure mat[n][n] as DS for storing the cost to the cell mat[i][j].
  2. Consider that you can visit current cell mat[i][j] only from left or top cell (if it possible).
  3. The answer is the value of mat[-1][-1].

Tarea

Create an algorithm to find the shortest path for the frog.

  1. Use data structure mat[n][n] as DS for storing the cost to the cell mat[i][j].
  2. Consider that you can visit current cell mat[i][j] only from left or top cell (if it possible).
  3. The answer is the value of mat[-1][-1].

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 2. Capítulo 2
toggle bottom row

Problem B. Minimum path

The tasks in this section contain test function calls. Please do not change this code; otherwise, the assignment may not be accepted.

Given a two-dimensional array mat with values in it. Each value means the price we should pay for entering it. There is frog sitting in the top-left cell who wants to move to top-right. The frog can move to the nearest cell either right or down per one move. When it enters a cell, frog has to pay mat[i][j] for visiting it. Your goal is to find a path with minimal price. Return the cost of such a path.

Example 1

The orange path is minimum and costs 25.

Example 2

Input :

[[1, 3, 4],

[2, 1, 5],

[4, 6, 7]]

Output: 16

The path looks like:

Example 3

Input:

[[1, 2, 4],

[8, 5, 1]]

Output: 8

The Optimal Substructure here is to find the minimum path for each cell based on previous ones:

mat[i][j] = mat[i][j] + min(mat[i-1][j], mat[i][j-1])

This way, the minimum path to the mat[i][j] cell includes the price of this cell and the minimum price to one of the available cells (top or left).

Tarea

Create an algorithm to find the shortest path for the frog.

  1. Use data structure mat[n][n] as DS for storing the cost to the cell mat[i][j].
  2. Consider that you can visit current cell mat[i][j] only from left or top cell (if it possible).
  3. The answer is the value of mat[-1][-1].

Tarea

Create an algorithm to find the shortest path for the frog.

  1. Use data structure mat[n][n] as DS for storing the cost to the cell mat[i][j].
  2. Consider that you can visit current cell mat[i][j] only from left or top cell (if it possible).
  3. The answer is the value of mat[-1][-1].

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 2. Capítulo 2
toggle bottom row

Problem B. Minimum path

The tasks in this section contain test function calls. Please do not change this code; otherwise, the assignment may not be accepted.

Given a two-dimensional array mat with values in it. Each value means the price we should pay for entering it. There is frog sitting in the top-left cell who wants to move to top-right. The frog can move to the nearest cell either right or down per one move. When it enters a cell, frog has to pay mat[i][j] for visiting it. Your goal is to find a path with minimal price. Return the cost of such a path.

Example 1

The orange path is minimum and costs 25.

Example 2

Input :

[[1, 3, 4],

[2, 1, 5],

[4, 6, 7]]

Output: 16

The path looks like:

Example 3

Input:

[[1, 2, 4],

[8, 5, 1]]

Output: 8

The Optimal Substructure here is to find the minimum path for each cell based on previous ones:

mat[i][j] = mat[i][j] + min(mat[i-1][j], mat[i][j-1])

This way, the minimum path to the mat[i][j] cell includes the price of this cell and the minimum price to one of the available cells (top or left).

Tarea

Create an algorithm to find the shortest path for the frog.

  1. Use data structure mat[n][n] as DS for storing the cost to the cell mat[i][j].
  2. Consider that you can visit current cell mat[i][j] only from left or top cell (if it possible).
  3. The answer is the value of mat[-1][-1].

Tarea

Create an algorithm to find the shortest path for the frog.

  1. Use data structure mat[n][n] as DS for storing the cost to the cell mat[i][j].
  2. Consider that you can visit current cell mat[i][j] only from left or top cell (if it possible).
  3. The answer is the value of mat[-1][-1].

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

The tasks in this section contain test function calls. Please do not change this code; otherwise, the assignment may not be accepted.

Given a two-dimensional array mat with values in it. Each value means the price we should pay for entering it. There is frog sitting in the top-left cell who wants to move to top-right. The frog can move to the nearest cell either right or down per one move. When it enters a cell, frog has to pay mat[i][j] for visiting it. Your goal is to find a path with minimal price. Return the cost of such a path.

Example 1

The orange path is minimum and costs 25.

Example 2

Input :

[[1, 3, 4],

[2, 1, 5],

[4, 6, 7]]

Output: 16

The path looks like:

Example 3

Input:

[[1, 2, 4],

[8, 5, 1]]

Output: 8

The Optimal Substructure here is to find the minimum path for each cell based on previous ones:

mat[i][j] = mat[i][j] + min(mat[i-1][j], mat[i][j-1])

This way, the minimum path to the mat[i][j] cell includes the price of this cell and the minimum price to one of the available cells (top or left).

Tarea

Create an algorithm to find the shortest path for the frog.

  1. Use data structure mat[n][n] as DS for storing the cost to the cell mat[i][j].
  2. Consider that you can visit current cell mat[i][j] only from left or top cell (if it possible).
  3. The answer is the value of mat[-1][-1].

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 2
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt