1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/execution-of-all-suffix-instructions-staying-in-a-grid.json

174 lines
19 KiB
JSON

{
"data": {
"question": {
"questionId": "2239",
"questionFrontendId": "2120",
"boundTopicId": null,
"title": "Execution of All Suffix Instructions Staying in a Grid",
"titleSlug": "execution-of-all-suffix-instructions-staying-in-a-grid",
"content": "<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>\n\n<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>&#39;L&#39;</code> (move left), <code>&#39;R&#39;</code> (move right), <code>&#39;U&#39;</code> (move up), and <code>&#39;D&#39;</code> (move down).</p>\n\n<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>\n\n<ul>\n\t<li>The next instruction will move the robot off the grid.</li>\n\t<li>There are no more instructions left to execute.</li>\n</ul>\n\n<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>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/09/1.png\" style=\"width: 145px; height: 142px;\" />\n<pre>\n<strong>Input:</strong> n = 3, startPos = [0,1], s = &quot;RRDDLU&quot;\n<strong>Output:</strong> [1,5,4,3,1,0]\n<strong>Explanation:</strong> Starting from startPos and beginning execution from the i<sup>th</sup> instruction:\n- 0<sup>th</sup>: &quot;<u><strong>R</strong></u>RDDLU&quot;. Only one instruction &quot;R&quot; can be executed before it moves off the grid.\n- 1<sup>st</sup>: &quot;<u><strong>RDDLU</strong></u>&quot;. All five instructions can be executed while it stays in the grid and ends at (1, 1).\n- 2<sup>nd</sup>: &quot;<u><strong>DDLU</strong></u>&quot;. All four instructions can be executed while it stays in the grid and ends at (1, 0).\n- 3<sup>rd</sup>: &quot;<u><strong>DLU</strong></u>&quot;. All three instructions can be executed while it stays in the grid and ends at (0, 0).\n- 4<sup>th</sup>: &quot;<u><strong>L</strong></u>U&quot;. Only one instruction &quot;L&quot; can be executed before it moves off the grid.\n- 5<sup>th</sup>: &quot;U&quot;. If moving up, it would move off the grid.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/09/2.png\" style=\"width: 106px; height: 103px;\" />\n<pre>\n<strong>Input:</strong> n = 2, startPos = [1,1], s = &quot;LURD&quot;\n<strong>Output:</strong> [4,1,0,0]\n<strong>Explanation:</strong>\n- 0<sup>th</sup>: &quot;<u><strong>LURD</strong></u>&quot;.\n- 1<sup>st</sup>: &quot;<u><strong>U</strong></u>RD&quot;.\n- 2<sup>nd</sup>: &quot;RD&quot;.\n- 3<sup>rd</sup>: &quot;D&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/09/3.png\" style=\"width: 67px; height: 64px;\" />\n<pre>\n<strong>Input:</strong> n = 1, startPos = [0,0], s = &quot;LRUD&quot;\n<strong>Output:</strong> [0,0,0,0]\n<strong>Explanation:</strong> No matter which instruction the robot begins execution from, it would move off the grid.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == s.length</code></li>\n\t<li><code>1 &lt;= n, m &lt;= 500</code></li>\n\t<li><code>startPos.length == 2</code></li>\n\t<li><code>0 &lt;= start<sub>row</sub>, start<sub>col</sub> &lt; n</code></li>\n\t<li><code>s</code> consists of <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, <code>&#39;U&#39;</code>, and <code>&#39;D&#39;</code>.</li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 509,
"dislikes": 47,
"isLiked": null,
"similarQuestions": "[{\"title\": \"Out of Boundary Paths\", \"titleSlug\": \"out-of-boundary-paths\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Robot Return to Origin\", \"titleSlug\": \"robot-return-to-origin\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]",
"exampleTestcases": "3\n[0,1]\n\"RRDDLU\"\n2\n[1,1]\n\"LURD\"\n1\n[0,0]\n\"LRUD\"",
"categoryTitle": "Algorithms",
"contributors": [],
"topicTags": [
{
"name": "String",
"slug": "string",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Simulation",
"slug": "simulation",
"translatedName": null,
"__typename": "TopicTagNode"
}
],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "C++",
"langSlug": "cpp",
"code": "class Solution {\npublic:\n vector<int> executeInstructions(int n, vector<int>& startPos, string s) {\n \n }\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Java",
"langSlug": "java",
"code": "class Solution {\n public int[] executeInstructions(int n, int[] startPos, String s) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python",
"langSlug": "python",
"code": "class Solution(object):\n def executeInstructions(self, n, startPos, s):\n \"\"\"\n :type n: int\n :type startPos: List[int]\n :type s: str\n :rtype: List[int]\n \"\"\"\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python3",
"langSlug": "python3",
"code": "class Solution:\n def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "C",
"langSlug": "c",
"code": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* executeInstructions(int n, int* startPos, int startPosSize, char* s, int* returnSize) {\n \n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "C#",
"langSlug": "csharp",
"code": "public class Solution {\n public int[] ExecuteInstructions(int n, int[] startPos, string s) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {number} n\n * @param {number[]} startPos\n * @param {string} s\n * @return {number[]}\n */\nvar executeInstructions = function(n, startPos, s) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function executeInstructions(n: number, startPos: number[], s: string): number[] {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "PHP",
"langSlug": "php",
"code": "class Solution {\n\n /**\n * @param Integer $n\n * @param Integer[] $startPos\n * @param String $s\n * @return Integer[]\n */\n function executeInstructions($n, $startPos, $s) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Swift",
"langSlug": "swift",
"code": "class Solution {\n func executeInstructions(_ n: Int, _ startPos: [Int], _ s: String) -> [Int] {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Kotlin",
"langSlug": "kotlin",
"code": "class Solution {\n fun executeInstructions(n: Int, startPos: IntArray, s: String): IntArray {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Dart",
"langSlug": "dart",
"code": "class Solution {\n List<int> executeInstructions(int n, List<int> startPos, String s) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Go",
"langSlug": "golang",
"code": "func executeInstructions(n int, startPos []int, s string) []int {\n \n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Ruby",
"langSlug": "ruby",
"code": "# @param {Integer} n\n# @param {Integer[]} start_pos\n# @param {String} s\n# @return {Integer[]}\ndef execute_instructions(n, start_pos, s)\n \nend",
"__typename": "CodeSnippetNode"
},
{
"lang": "Scala",
"langSlug": "scala",
"code": "object Solution {\n def executeInstructions(n: Int, startPos: Array[Int], s: String): Array[Int] = {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Rust",
"langSlug": "rust",
"code": "impl Solution {\n pub fn execute_instructions(n: i32, start_pos: Vec<i32>, s: String) -> Vec<i32> {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Racket",
"langSlug": "racket",
"code": "(define/contract (execute-instructions n startPos s)\n (-> exact-integer? (listof exact-integer?) string? (listof exact-integer?))\n )",
"__typename": "CodeSnippetNode"
},
{
"lang": "Erlang",
"langSlug": "erlang",
"code": "-spec execute_instructions(N :: integer(), StartPos :: [integer()], S :: unicode:unicode_binary()) -> [integer()].\nexecute_instructions(N, StartPos, S) ->\n .",
"__typename": "CodeSnippetNode"
},
{
"lang": "Elixir",
"langSlug": "elixir",
"code": "defmodule Solution do\n @spec execute_instructions(n :: integer, start_pos :: [integer], s :: String.t) :: [integer]\n def execute_instructions(n, start_pos, s) do\n \n end\nend",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"27.9K\", \"totalSubmission\": \"33.8K\", \"totalAcceptedRaw\": 27940, \"totalSubmissionRaw\": 33837, \"acRate\": \"82.6%\"}",
"hints": [
"The constraints are not very large. Can we simulate the execution by starting from each index of s?",
"Before any of the stopping conditions is met, stop the simulation for that index and set the answer for that index."
],
"solution": null,
"status": null,
"sampleTestCase": "3\n[0,1]\n\"RRDDLU\"",
"metaData": "{\n \"name\": \"executeInstructions\",\n \"params\": [\n {\n \"name\": \"n\",\n \"type\": \"integer\"\n },\n {\n \"type\": \"integer[]\",\n \"name\": \"startPos\"\n },\n {\n \"type\": \"string\",\n \"name\": \"s\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\"\n }\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": true,
"envInfo": "{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 11 </code> using the latest C++ 20 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\"], \"java\": [\"Java\", \"<p><code>OpenJDK 17</code>. Java 8 features such as lambda expressions and stream API can be used. </p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>\"], \"python\": [\"Python\", \"<p><code>Python 2.7.12</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/2/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/2/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/2/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\\r\\n\\r\\n<p>Note that Python 2.7 <a href=\\\"https://www.python.org/dev/peps/pep-0373/\\\" target=\\\"_blank\\\">will not be maintained past 2020</a>. For the latest Python, please choose Python3 instead.</p>\"], \"c\": [\"C\", \"<p>Compiled with <code>gcc 8.2</code> using the gnu11 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level one optimization (<code>-O1</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n\\r\\n<p>For hash table operations, you may use <a href=\\\"https://troydhanson.github.io/uthash/\\\" target=\\\"_blank\\\">uthash</a>. \\\"uthash.h\\\" is included by default. Below are some examples:</p>\\r\\n\\r\\n<p><b>1. Adding an item to a hash.</b>\\r\\n<pre>\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>2. Looking up an item in a hash:</b>\\r\\n<pre>\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>3. Deleting an item in a hash:</b>\\r\\n<pre>\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n</pre>\\r\\n</p>\"], \"csharp\": [\"C#\", \"<p><a href=\\\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10\\\" target=\\\"_blank\\\">C# 10 with .NET 6 runtime</a></p>\"], \"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"ruby\": [\"Ruby\", \"<p><code>Ruby 3.1</code></p>\\r\\n\\r\\n<p>Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms</p>\"], \"swift\": [\"Swift\", \"<p><code>Swift 5.5.2</code>.</p>\"], \"golang\": [\"Go\", \"<p><code>Go 1.21</code></p>\\r\\n<p>Support <a href=\\\"https://github.com/emirpasic/gods/tree/v1.18.1\\\" target=\\\"_blank\\\">https://godoc.org/github.com/emirpasic/gods@v1.18.1</a> library.</p>\"], \"python3\": [\"Python3\", \"<p><code>Python 3.10</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/3/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/3/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/3/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\"], \"scala\": [\"Scala\", \"<p><code>Scala 2.13.7</code>.</p>\"], \"kotlin\": [\"Kotlin\", \"<p><code>Kotlin 1.9.0</code>.</p>\"], \"rust\": [\"Rust\", \"<p><code>Rust 1.58.1</code></p>\\r\\n\\r\\n<p>Supports <a href=\\\"https://crates.io/crates/rand\\\" target=\\\"_blank\\\">rand </a> v0.6\\u00a0from crates.io</p>\"], \"php\": [\"PHP\", \"<p><code>PHP 8.1</code>.</p>\\r\\n<p>With bcmath module</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 5.1.6, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2022 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"], \"racket\": [\"Racket\", \"<p>Run with <code>Racket 8.3</code>.</p>\"], \"erlang\": [\"Erlang\", \"Erlang/OTP 25.0\"], \"elixir\": [\"Elixir\", \"Elixir 1.13.4 with Erlang/OTP 25.0\"], \"dart\": [\"Dart\", \"<p>Dart 2.17.3</p>\\r\\n\\r\\n<p>Your code will be run directly without compiling</p>\"]}",
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}