Similar Problems
Similar Problems not available
Minimum Cost To Make At Least One Valid Path In A Grid - Leetcode Solution
Companies:
LeetCode: Minimum Cost To Make At Least One Valid Path In A Grid Leetcode Solution
Difficulty: Hard
Topics: breadth-first-search matrix heap-priority-queue array graph
Problem:
You are given a m x n grid. Each cell of the grid represents a street. The street of grid[i][j] can be:
1 which means a street connecting the left cell and the right cell. 2 which means a street connecting the upper cell and the lower cell. 3 which means a street connecting the left cell and the lower cell. 4 which means a street connecting the right cell and the lower cell. 5 which means a street connecting the left cell and the upper cell. 6 which means a street connecting the right cell and the upper cell.
You will initially start at the street of grid[0][0]. The goal is to reach the street of grid[m - 1][n - 1].
You will be given an integer maxMoves. The number of moves you can make is limited to maxMoves.
Return the number of paths to reach the destination. Since the answer can be very large, return it modulo 10^9 + 7.
Also, return the minimum cost to make at least one valid path, or -1 if it is impossible to reach the destination.
Solution:
The given problem can be solved using dynamic programming approach. We can make a dp array where at dp[i][j][k] we will store the minimum cost to reach from (0,0) to cell (i,j) in k moves.
If the current street is 1, we can move to either left or right cell but not to the upper or lower cell. Thus, we update dp[i][j][k] as the minimum of dp[i][j][k] and dp[i][j - 1][k - 1] + cost of moving right and dp[i][j + 1][k - 1] + cost of moving left.
Similarly, for the street of type 2, we can move to either upper or lower cell but not to the left or right cell. Thus, we update dp[i][j][k] as the minimum of dp[i][j][k] and dp[i - 1][j][k - 1] + cost of moving down and dp[i + 1][j][k - 1] + cost of moving up.
For type 3 street, we can move to either left or lower cell, and for type 4 street, we can move to either right or lower cell. Similarly, for type 5 street, we can move to either left or upper cell and for type 6 street, we can move to either right or upper cell.
We will update dp[i][j][k] for each of the above cases and take the minimum cost as the minimum of all the values of dp[m - 1][n - 1][k] where k <= maxMoves.
Code:
Below is the Python code for our solution:
class Solution: def findPaths(self, m: int, n: int, maxMove: int, startRow: int, startColumn: int) -> int: mod = 10 ** 9 + 7 dp = [[[float('inf')] * (maxMove + 1) for _ in range(n)] for _ in range(m)]
# initializing starting cell
for k in range(maxMove + 1):
dp[startRow][startColumn][k] = 0
# updating dp array
for k in range(1, maxMove + 1):
for i in range(m):
for j in range(n):
if i > 0:
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k - 1] + 1)
if j > 0:
dp[i][j][k] = min(dp[i][j][k], dp[i][j - 1][k - 1] + 1)
if i < m - 1:
dp[i][j][k] = min(dp[i][j][k], dp[i + 1][j][k - 1] + 1)
if j < n - 1:
dp[i][j][k] = min(dp[i][j][k], dp[i][j + 1][k - 1] + 1)
# finding minimum cost to reach destination
minCost = float('inf')
for k in range(1, maxMove + 1):
minCost = min(minCost, dp[m - 1][n - 1][k])
# finding number of paths to reach destination
res = 0
for k in range(1, maxMove + 1):
for i in range(m):
for j in range(n):
if i == 0 or i == m - 1 or j == 0 or j == n - 1:
if dp[i][j][k] < float('inf'):
res = (res + dp[i][j][k]) % mod
if minCost == float('inf'):
return -1
return res, minCost
Time Complexity:
The time complexity of the above solution is O(mnmaxMove), where m is the number of rows, n is the number of columns and maxMove is the maximum number of moves allowed.
Space Complexity:
The space complexity of the above solution is also O(mnmaxMove), as we are using a 3D array for dp.
Minimum Cost To Make At Least One Valid Path In A Grid Solution Code
1