Skip to content

Commit 96c77c9

Browse files
add 2120
1 parent 3f9624e commit 96c77c9

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|2120|[Execution of All Suffix Instructions Staying in a Grid](https://leetcode.com/problems/execution-of-all-suffix-instructions-staying-in-a-grid/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2120.java) ||Medium||
1112
|2119|[A Number After a Double Reversal](https://leetcode.com/problems/a-number-after-a-double-reversal/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2119.java) ||Easy||
1213
|2116|[Check if a Parentheses String Can Be Valid](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2116.java) ||Medium||
1314
|2114|[Maximum Number of Words Found in Sentences](https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/)|[Java](../master/src/main/java/com/fishercoder/solutions/_2114.java) ||Easy||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _2120 {
4+
public static class Solution1 {
5+
public int[] executeInstructions(int n, int[] startPos, String s) {
6+
int[] ans = new int[s.length()];
7+
for (int i = 0; i < s.length(); i++) {
8+
int y = startPos[1];
9+
int x = startPos[0];
10+
int j = i;
11+
boolean broken = false;
12+
for (; j < s.length(); j++) {
13+
if (s.charAt(j) == 'R') {
14+
if (y + 1 < n) {
15+
y++;
16+
} else {
17+
ans[i] = j - i;
18+
broken = true;
19+
break;
20+
}
21+
} else if (s.charAt(j) == 'L') {
22+
if (y - 1 >= 0) {
23+
y--;
24+
} else {
25+
ans[i] = j - i;
26+
broken = true;
27+
break;
28+
}
29+
} else if (s.charAt(j) == 'U') {
30+
if (x - 1 >= 0) {
31+
x--;
32+
} else {
33+
ans[i] = j - i;
34+
broken = true;
35+
break;
36+
}
37+
} else if (s.charAt(j) == 'D') {
38+
if (x + 1 < n) {
39+
x++;
40+
} else {
41+
ans[i] = j - i;
42+
broken = true;
43+
break;
44+
}
45+
}
46+
}
47+
if (!broken) {
48+
ans[i] = j - i;
49+
}
50+
}
51+
return ans;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)