|
| 1 | +/** |
| 2 | + * [2120] Execution of All Suffix Instructions Staying in a Grid |
| 3 | + * |
| 4 | + * There is an n x n grid, with the top-left cell at (0, 0) and the bottom-right cell at (n - 1, n - 1). You are given the integer n and an integer array startPos where startPos = [startrow, startcol] indicates that a robot is initially at cell (startrow, startcol). |
| 5 | + * You are also given a 0-indexed string s of length m where s[i] is the i^th instruction for the robot: 'L' (move left), 'R' (move right), 'U' (move up), and 'D' (move down). |
| 6 | + * The robot can begin executing from any i^th instruction in s. It executes the instructions one by one towards the end of s but it stops if either of these conditions is met: |
| 7 | + * |
| 8 | + * The next instruction will move the robot off the grid. |
| 9 | + * There are no more instructions left to execute. |
| 10 | + * |
| 11 | + * Return an array answer of length m where answer[i] is the number of instructions the robot can execute if the robot begins executing from the i^th instruction in s. |
| 12 | + * |
| 13 | + * Example 1: |
| 14 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/1.png" style="width: 145px; height: 142px;" /> |
| 15 | + * Input: n = 3, startPos = [0,1], s = "RRDDLU" |
| 16 | + * Output: [1,5,4,3,1,0] |
| 17 | + * Explanation: Starting from startPos and beginning execution from the i^th instruction: |
| 18 | + * - 0^th: "<u>R</u>RDDLU". Only one instruction "R" can be executed before it moves off the grid. |
| 19 | + * - 1^st: "<u>RDDLU</u>". All five instructions can be executed while it stays in the grid and ends at (1, 1). |
| 20 | + * - 2^nd: "<u>DDLU</u>". All four instructions can be executed while it stays in the grid and ends at (1, 0). |
| 21 | + * - 3^rd: "<u>DLU</u>". All three instructions can be executed while it stays in the grid and ends at (0, 0). |
| 22 | + * - 4^th: "<u>L</u>U". Only one instruction "L" can be executed before it moves off the grid. |
| 23 | + * - 5^th: "U". If moving up, it would move off the grid. |
| 24 | + * |
| 25 | + * Example 2: |
| 26 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/2.png" style="width: 106px; height: 103px;" /> |
| 27 | + * Input: n = 2, startPos = [1,1], s = "LURD" |
| 28 | + * Output: [4,1,0,0] |
| 29 | + * Explanation: |
| 30 | + * - 0^th: "<u>LURD</u>". |
| 31 | + * - 1^st: "<u>U</u>RD". |
| 32 | + * - 2^nd: "RD". |
| 33 | + * - 3^rd: "D". |
| 34 | + * |
| 35 | + * Example 3: |
| 36 | + * <img alt="" src="https://assets.leetcode.com/uploads/2021/12/09/3.png" style="width: 67px; height: 64px;" /> |
| 37 | + * Input: n = 1, startPos = [0,0], s = "LRUD" |
| 38 | + * Output: [0,0,0,0] |
| 39 | + * Explanation: No matter which instruction the robot begins execution from, it would move off the grid. |
| 40 | + * |
| 41 | + * |
| 42 | + * Constraints: |
| 43 | + * |
| 44 | + * m == s.length |
| 45 | + * 1 <= n, m <= 500 |
| 46 | + * startPos.length == 2 |
| 47 | + * 0 <= startrow, startcol < n |
| 48 | + * s consists of 'L', 'R', 'U', and 'D'. |
| 49 | + * |
| 50 | + */ |
| 51 | +pub struct Solution {} |
| 52 | + |
| 53 | +// problem: https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/ |
| 54 | +// discuss: https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/discuss/?currentPage=1&orderBy=most_votes&query= |
| 55 | + |
| 56 | +// submission codes start here |
| 57 | + |
| 58 | +impl Solution { |
| 59 | + pub fn execute_instructions(n: i32, start_pos: Vec<i32>, s: String) -> Vec<i32> { |
| 60 | + vec![] |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// submission codes end |
| 65 | + |
| 66 | +#[cfg(test)] |
| 67 | +mod tests { |
| 68 | + use super::*; |
| 69 | + |
| 70 | + #[test] |
| 71 | + #[ignore] |
| 72 | + fn test_2120_example_1() { |
| 73 | + let n = 3; |
| 74 | + let start_pos = [0, 1]; |
| 75 | + let s = "RRDDLU".to_string(); |
| 76 | + |
| 77 | + let result = vec![1, 5, 4, 3, 1, 0]; |
| 78 | + |
| 79 | + assert_eq!(Solution::execute_instructions(n, start_pos, s), result); |
| 80 | + } |
| 81 | + |
| 82 | + #[test] |
| 83 | + #[ignore] |
| 84 | + fn test_2120_example_2() { |
| 85 | + let n = 2; |
| 86 | + let start_pos = [1, 1]; |
| 87 | + let s = "LURD".to_string(); |
| 88 | + |
| 89 | + let result = vec![4, 1, 0, 0]; |
| 90 | + |
| 91 | + assert_eq!(Solution::execute_instructions(n, start_pos, s), result); |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + #[ignore] |
| 96 | + fn test_2120_example_3() { |
| 97 | + let n = 1; |
| 98 | + let start_pos = [0, 0]; |
| 99 | + let s = "LRUD".to_string(); |
| 100 | + |
| 101 | + let result = vec![0, 0, 0, 0]; |
| 102 | + |
| 103 | + assert_eq!(Solution::execute_instructions(n, start_pos, s), result); |
| 104 | + } |
| 105 | +} |
0 commit comments