19- Paths in matrix
Description
Given a matrix of size n*m that contains only 0s and 1s, where 0 means that the cell is empty and 1 means that there is a wall in that cell, create a function that returns the number of paths that we can take to go from the top left cell to the right bottom cell, knowing that you can move to the right or to the bottom only.
Example 1:
- Input: matrix = [[0, 0, 0, 0, 1], [1, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0]]
- Output: 4
- 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