17- Minimum cost path in matrix
Description
Given a matrix of integers matrix of size n*m, where each element matrix[i][j] represents the cost of passing from that cell, create a function that returns the cost of the minimum cost path to go from the top left cell to the right bottom cell, knowing that you can only move to the right or to the bottom.
Example 1:
- Input: matrix = [[3, 12, 4, 7, 10], [6, 8, 15, 11, 4], [19, 5, 14, 32, 21], [1, 20, 2, 9, 7]]
- Output: 54
- Explanation:
Try some input:
Try to solve:
Select a language:
Solution:
Code:
Select a language:
Python code:
By using recursion:
In case the code doesn't appear: click here
Time complexity: O(2^(n+m))
Space complexity: O(n + m)
By using dynamic programming:
In case the code doesn't appear: click here
Time complexity: O(n*m)
Space complexity: O(n*m)
Java code:
By using recursion:
In case the code doesn't appear: click here
Time complexity: O(2^(n+m))
Space complexity: O(n + m)
By using dynamic programming:
In case the code doesn't appear: click here
Time complexity: O(n*m)
Space complexity: O(n*m)
C++ code:
By using recursion:
In case the code doesn't appear: click here
Time complexity: O(2^(n+m))
Space complexity: O(n + m)
By using dynamic programming:
In case the code doesn't appear: click here
Time complexity: O(n*m)
Space complexity: O(n*m)
JavaScript code:
By using recursion:
In case the code doesn't appear: click here
Time complexity: O(2^(n+m))
Space complexity: O(n + m)
By using dynamic programming:
In case the code doesn't appear: click here
Time complexity: O(n*m)
Space complexity: O(n*m)
Frequently asked questions:
No questions yet.
You can ask a question in comment section below.
0 comments