Similar Problems
Similar Problems not available
Execution Of All Suffix Instructions Staying In A Grid - Leetcode Solution
Companies:
LeetCode: Execution Of All Suffix Instructions Staying In A Grid Leetcode Solution
Difficulty: Medium
Topics: string simulation
Problem Statement:
Given an n x n grid, you are given a sequence of moves represented by a string path. You begin in the top-left square (0, 0) and your goal is to reach the bottom-right square (n - 1, n - 1).
The following moves are possible on the grid:
- 'H' (horizontally right): move right horizontally, i.e., go from (i, j) to (i, j + 1).
- 'V' (vertically down): move down vertically, i.e., go from (i, j) to (i + 1, j).
- 'D' (diagonally down-right): move diagonally down-right, i.e., go from (i, j) to (i + 1, j + 1).
While moving through the grid, you can only take suffixes of the path in which all instructions are of the same type (i.e., either all are 'H', all are 'V', or all are 'D').
Return true if it is possible to reach the destination in the grid following the given path, or false otherwise.
Solution:
The first observation we can make is that if any of the suffixes is not of the same type, then we cannot reach the destination. This is because every time we switch from one type to another, we have to start from the beginning. So, if we encounter a mismatch, we will have to abandon all the progress we have made so far and start again.
Therefore, we need to check if all the suffixes are of the same type. If they are, then we can reach the destination if and only if the path length is divisible by the size of the grid n.
To check if all the suffixes are of the same type, we can simply iterate over the string path, counting the number of moves of each type in each suffix. If we encounter a mismatch, we return false immediately. Otherwise, we continue iterating until the end of the string, and then return true if all the suffixes have the same type.
To check if the path length is divisible by n, we simply count the number of moves in the path, and return true if it is divisible by n.
The time complexity of this solution is O(N), where N is the length of the input string path, and the space complexity is O(1), as we are only using a constant amount of space to store our variables.
Here's the Python implementation of the above solution:
class Solution: def hasValidPath(self, grid: List[List[int]]) -> bool: m, n = len(grid), len(grid[0]) x, y, moves = 0, 0, '' while x != m - 1 or y != n - 1: if grid[x][y] == 1: if y + 1 < n and (grid[x][y + 1] == 1 or grid[x][y + 1] == 3): y += 1 moves += 'H' else: return False elif grid[x][y] == 2: if x + 1 < m and (grid[x + 1][y] == 2 or grid[x + 1][y] == 5): x += 1 moves += 'V' else: return False else: if y + 1 < n and (grid[x][y + 1] == 1 or grid[x][y + 1] == 3): y += 1 moves += 'H' elif x + 1 < m and (grid[x + 1][y] == 2 or grid[x + 1][y] == 5): x += 1 moves += 'V' else: return False return len(moves) % n == 0 and all([(moves[i] == moves[-1]) for i in range(len(moves) - 1)])
The above code uses a while loop to follow the path of the input grid. At each step, it checks if we can move in the direction specified by the current cell. If we can move in that direction, we add the corresponding move to the string moves and update the current position accordingly. If we cannot move in that direction, we return False.
After following the path, we check if all the suffixes are of the same type using a list comprehension and the all() function. Finally, we check if the length of the path is divisible by n, and return True if it is. Otherwise, we return False.
Execution Of All Suffix Instructions Staying In A Grid Solution Code
1