Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8b2704b

Browse files
committed
Add solution #2120
1 parent 151962b commit 8b2704b

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,757 LeetCode solutions in JavaScript
1+
# 1,758 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1623,6 +1623,7 @@
16231623
2116|[Check if a Parentheses String Can Be Valid](./solutions/2116-check-if-a-parentheses-string-can-be-valid.js)|Medium|
16241624
2117|[Abbreviating the Product of a Range](./solutions/2117-abbreviating-the-product-of-a-range.js)|Hard|
16251625
2119|[A Number After a Double Reversal](./solutions/2119-a-number-after-a-double-reversal.js)|Easy|
1626+
2120|[Execution of All Suffix Instructions Staying in a Grid](./solutions/2120-execution-of-all-suffix-instructions-staying-in-a-grid.js)|Medium|
16261627
2127|[Maximum Employees to Be Invited to a Meeting](./solutions/2127-maximum-employees-to-be-invited-to-a-meeting.js)|Hard|
16271628
2129|[Capitalize the Title](./solutions/2129-capitalize-the-title.js)|Easy|
16281629
2130|[Maximum Twin Sum of a Linked List](./solutions/2130-maximum-twin-sum-of-a-linked-list.js)|Medium|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 2120. Execution of All Suffix Instructions Staying in a Grid
3+
* https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/
4+
* Difficulty: Medium
5+
*
6+
* There is an n x n grid, with the top-left cell at (0, 0) and the bottom-right cell at
7+
* (n - 1, n - 1). You are given the integer n and an integer array startPos where
8+
* startPos = [startrow, startcol] indicates that a robot is initially at cell (startrow, startcol).
9+
*
10+
* You are also given a 0-indexed string s of length m where s[i] is the ith instruction for the
11+
* robot: 'L' (move left), 'R' (move right), 'U' (move up), and 'D' (move down).
12+
*
13+
* The robot can begin executing from any ith instruction in s. It executes the instructions one
14+
* by one towards the end of s but it stops if either of these conditions is met:
15+
* - The next instruction will move the robot off the grid.
16+
* - There are no more instructions left to execute.
17+
*
18+
* Return an array answer of length m where answer[i] is the number of instructions the robot can
19+
* execute if the robot begins executing from the ith instruction in s.
20+
*/
21+
22+
/**
23+
* @param {number} n
24+
* @param {number[]} startPos
25+
* @param {string} s
26+
* @return {number[]}
27+
*/
28+
var executeInstructions = function(n, startPos, s) {
29+
const m = s.length;
30+
const result = new Array(m).fill(0);
31+
32+
for (let i = 0; i < m; i++) {
33+
let row = startPos[0];
34+
let col = startPos[1];
35+
let steps = 0;
36+
37+
for (let j = i; j < m; j++) {
38+
if (s[j] === 'L') col--;
39+
else if (s[j] === 'R') col++;
40+
else if (s[j] === 'U') row--;
41+
else row++;
42+
43+
if (row < 0 || row >= n || col < 0 || col >= n) break;
44+
steps++;
45+
}
46+
47+
result[i] = steps;
48+
}
49+
50+
return result;
51+
};

0 commit comments

Comments
 (0)