ShareChat Coding Question – Solved

6 Live
A forklift operator navigates products within an automotive parts warehouse. The dashboard displays a real-time map showing open and blocked sections as an n x m matrix of 1's (open) and 0's (blocked). The operator starts at the top-left corner of the map at warehouse[0][0] and aims to reach the bottom-right corner at warehouse[n-1][m-1]. Movements can only be made to the right or downward. Given the warehouse map, calculate the number of distinct paths from warehouse[0][0] to warehouse[n-1][m-1]. Return the result modulo (10⁹+7). Example warehouse = [ [1, 1, 0, 1], [1, 1, 1, 1] ] The matrix below is drawn from the warehouse array, showing open and blocked sections of the warehouse. 1 indicates an open section, and 0 indicates a blocked section. It is only possible to travel through open sections, so no path can go through the section at (0, 2).

Asked in: ShareChat

Image of the Question

Question Image

All Testcases Passed ✔



Passcode Image

Solution


import math
import os
import random
import re
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve the problem of finding the number of distinct paths from the top-left corner to the bottom-right corner in a grid where some cells are blocked, and movements are restricted to only right or down, you need to carefully analyze the constraints and structure of the grid. Here’s a detailed approach to thinking through this problem:

Step 1: Understand the problem setting
- You are given a 2D grid (matrix) of size n x m.
- Each cell can either be open (1) or blocked (0).
- You start at the top-left corner, position (0,0), and want to reach the bottom-right corner, position (n-1,m-1).
- Moves are allowed only in two directions: right (increment column by 1) or down (increment row by 1).
- You cannot pass through blocked cells.
- Your goal is to find the total number of distinct paths from start to end under these constraints.
- Since the number of paths can be large, you return the count modulo 10^9 + 7.

Step 2: Constraints and implications
- Movement constraints reduce path possibilities, eliminating any backtracking or diagonal moves.
- Blocked cells act as obstacles that prohibit certain routes.
- The path count is effectively the count of all sequences of right and down moves that reach the destination without hitting blocked cells.
- The problem resembles classic grid path counting with obstacles.

Step 3: Model the problem using dynamic programming (DP)
- Dynamic programming is a natural fit here because the number of ways to reach any cell depends on the number of ways to reach the cells immediately above it and to its left.
- For a cell at (i, j), if it’s open, the number of ways to reach it is the sum of ways to reach (i-1, j) and (i, j-1).
- If the cell is blocked, ways to reach it is zero since you cannot pass through it.
- Base case: The starting position (0,0) has one way to be reached if it’s open; zero if blocked.

Step 4: Define the DP state
- Create a 2D array dp of the same size as the warehouse grid.
- dp[i][j] represents the number of ways to reach cell (i, j) from the starting point.
- Initialize dp with zeros.
- Set dp[0][0] = 1 if warehouse[0][0] is open; else 0.

Step 5: Fill dp table iteratively
- Traverse each cell row by row and column by column.
- For each cell, check if it is open.
- If open:
- Add dp[i-1][j] to dp[i][j] if i-1 >= 0 (the cell above).
- Add dp[i][j-1] to dp[i][j] if j-1 >= 0 (the cell to the left).
- If blocked, dp[i][j] remains 0.
- Take modulo 10^9 + 7 after each addition to keep numbers manageable.

Step 6: Edge cases to consider
- If the starting cell or ending cell is blocked, no paths exist, return 0 immediately.
- If the grid is 1x1 and open, the answer is 1.
- Rows or columns with all blocked cells will cause zero paths beyond that point.
- The grid might be narrow or long (e.g., 1 row or 1 column), test those cases to verify correct behavior.

Step 7: Computational complexity and feasibility
- The DP solution runs in O(n*m) time, where n is the number of rows and m is the number of columns.
- It uses O(n*m) space for dp, which is feasible for typical constraints.
- The use of modulo operations prevents integer overflow for large inputs.

Step 8: Optional space optimization
- Since dp[i][j] depends only on dp[i-1][j] and dp[i][j-1], you can optimize space by maintaining only the current and previous row.
- This reduces memory consumption from O(n*m) to O(m).

Step 9: Implementation considerations
- Initialize the dp array carefully, ensuring the first row and first column are properly handled because they have only one possible way to be reached (all right moves or all down moves) if no blocks interrupt.
- Carefully implement boundary checks for indices.
- Make sure to perform modulo operations consistently after each addition.

Step 10: Example walkthrough
- For the given example:
warehouse = [[1,1,0,1],[1,1,1,1]]
- The cell at (0,2) is blocked, so paths cannot pass through it.
- Start at (0,0) with dp[0][0]=1.
- Compute dp for first row: dp[0][1]=1 (since warehouse[0][1] is open), dp[0][2]=0 (blocked), dp[0][3]=0 (because (0,2) is blocked, no paths can reach (0,3) through row 0).
- Compute dp for second row similarly using sums of top and left cells.
- Sum of dp[n-1][m-1] gives total distinct paths modulo 10^9+7.

Summary:
- Convert the grid into a DP table.
- Use known relationships for number of paths in a grid with obstacles.
- Carefully handle blocked cells and boundaries.
- Use modulo arithmetic to manage large results.
- This approach guarantees correct count of all possible paths while respecting movement and obstacle constraints.
```


Related Questions