java.lang.Object
g2101_2200.s2120_execution_of_all_suffix_instructions_staying_in_a_grid.Solution

public class Solution extends Object
2120 - Execution of All Suffix Instructions Staying in a Grid.<p>Medium</p> <p>There is an <code>n x n</code> grid, with the top-left cell at <code>(0, 0)</code> and the bottom-right cell at <code>(n - 1, n - 1)</code>. You are given the integer <code>n</code> and an integer array <code>startPos</code> where <code>startPos = [start<sub>row</sub>, start<sub>col</sub>]</code> indicates that a robot is initially at cell <code>(start<sub>row</sub>, start<sub>col</sub>)</code>.</p> <p>You are also given a <strong>0-indexed</strong> string <code>s</code> of length <code>m</code> where <code>s[i]</code> is the <code>i<sup>th</sup></code> instruction for the robot: <code>'L'</code> (move left), <code>'R'</code> (move right), <code>'U'</code> (move up), and <code>'D'</code> (move down).</p> <p>The robot can begin executing from any <code>i<sup>th</sup></code> instruction in <code>s</code>. It executes the instructions one by one towards the end of <code>s</code> but it stops if either of these conditions is met:</p> <ul> <li>The next instruction will move the robot off the grid.</li> <li>There are no more instructions left to execute.</li> </ul> <p>Return <em>an array</em> <code>answer</code> <em>of length</em> <code>m</code> <em>where</em> <code>answer[i]</code> <em>is <strong>the number of instructions</strong> the robot can execute if the robot <strong>begins executing from</strong> the</em> <code>i<sup>th</sup></code> <em>instruction in</em> <code>s</code>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/09/1.png" alt="" /></p> <p><strong>Input:</strong> n = 3, startPos = [0,1], s = &ldquo;RRDDLU&rdquo;</p> <p><strong>Output:</strong> [1,5,4,3,1,0]</p> <p><strong>Explanation:</strong> Starting from startPos and beginning execution from the i<sup>th</sup> instruction:</p> <ul> <li> <p>0<sup>th</sup>: &ldquo;<strong>R</strong>RDDLU&rdquo;. Only one instruction &ldquo;R&rdquo; can be executed before it moves off the grid.</p> </li> <li> <p>1<sup>st</sup>: &ldquo;<strong>RDDLU</strong>&rdquo;. All five instructions can be executed while it stays in the grid and ends at (1, 1).</p> </li> <li> <p>2<sup>nd</sup>: &ldquo;<strong>DDLU</strong>&rdquo;. All four instructions can be executed while it stays in the grid and ends at (1, 0).</p> </li> <li> <p>3<sup>rd</sup>: &ldquo;<strong>DLU</strong>&rdquo;. All three instructions can be executed while it stays in the grid and ends at (0, 0).</p> </li> <li> <p>4<sup>th</sup>: &ldquo;<strong>L</strong>U&rdquo;. Only one instruction &ldquo;L&rdquo; can be executed before it moves off the grid.</p> </li> <li> <p>5<sup>th</sup>: &ldquo;U&rdquo;. If moving up, it would move off the grid.</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/09/2.png" alt="" /></p> <p><strong>Input:</strong> n = 2, startPos = [1,1], s = &ldquo;LURD&rdquo;</p> <p><strong>Output:</strong> [4,1,0,0]</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>0<sup>th</sup>: &ldquo;<strong>LURD</strong>&rdquo;.</p> </li> <li> <p>1<sup>st</sup>: &ldquo;<strong>U</strong>RD&rdquo;.</p> </li> <li> <p>2<sup>nd</sup>: &ldquo;RD&rdquo;.</p> </li> <li> <p>3<sup>rd</sup>: &ldquo;D&rdquo;.</p> </li> </ul> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/12/09/3.png" alt="" /></p> <p><strong>Input:</strong> n = 1, startPos = [0,0], s = &ldquo;LRUD&rdquo;</p> <p><strong>Output:</strong> [0,0,0,0]</p> <p><strong>Explanation:</strong> No matter which instruction the robot begins execution from, it would move off the grid.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == s.length</code></li> <li><code>1 <= n, m <= 500</code></li> <li><code>startPos.length == 2</code></li> <li><code>0 <= start<sub>row</sub>, start<sub>col</sub> < n</code></li> <li><code>s</code> consists of <code>'L'</code>, <code>'R'</code>, <code>'U'</code>, and <code>'D'</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • executeInstructions

      public int[] executeInstructions(int n, int[] startPos, String s)