Problem · Matrix

Reach the End in Time

Learn this problem
EasyGoogleOA
See Google hiring insights

Problem statement

A 2-D grid consisting of some blocked (represented as '#') and some unblocked (represented as '.') cells is given. The starting position of a pointer is in the top-left corner of the grid. It is guaranteed that the starting position is in an unblocked cell, and it is also guaranteed that the bottom-right cell is unblocked. Each cell of the grid is connected with its right, left, top, and bottom cells (if those cells exist). It takes 1 second for a pointer to move from a cell to its adjacent cell. If the pointer can reach the bottom-right corner of the grid within maxTime seconds, return the string 'Yes'. Otherwise, return the string 'No'.

Function

reachTheEnd(grid: String[], maxTime: int) → String

Complete the function reachTheEnd in the editor.

reachTheEnd has the following parameter(s):

  1. String[] grid: an array of strings representing the rows of the grid
  2. int maxTime: the maximum time to complete the journey

Examples

Example 1

grid = ["..##", "#.##", "#..."]maxTime = 5return = "Yes"
The shortest path has length 5, so the destination is reachable within the deadline.

Example 2

grid = ["..", ".."]maxTime = 3return = "Yes"
The shortest path has length 2, which is at most 3.

Example 3

grid = [".#", "#."]maxTime = 2return = "No"
Both possible first moves are blocked, so the destination is unreachable.

Constraints

  • 1 ≤ grid.length, grid[i].length ≤ 500
  • All rows have equal length and contain only . and #.
  • The top-left and bottom-right cells are unblocked.
  • 0 ≤ maxTime ≤ 10^5

More Google problems

drafts saved locally
public String reachTheEnd(String[] grid, int maxTime) {
  // write your code here
}
grid["..##", "#.##", "#..."]
maxTime5
expected"Yes"
checking account