Skip to content

Commit d8a6a1f

Browse files
authored
Create Execution of All Suffix Instructions Staying in a Grid.java
1 parent 569f60d commit d8a6a1f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int[] executeInstructions(int n, int[] startPos, String s) {
3+
int[] result = new int[s.length()];
4+
Map<Character, int[]> directionToMovementMap = Map.of(
5+
'R', new int[]{0, 1},
6+
'L', new int[]{0, -1},
7+
'U', new int[]{-1, 0},
8+
'D', new int[]{1, 0}
9+
);
10+
for (int i = 0; i < s.length(); i++) {
11+
int startX = startPos[0];
12+
int startY = startPos[1];
13+
int j = i;
14+
for (; j < s.length(); j++) {
15+
startX += directionToMovementMap.get(s.charAt(j))[0];
16+
startY += directionToMovementMap.get(s.charAt(j))[1];
17+
if (startX < 0 || startY < 0 || startX >= n || startY >= n) {
18+
break;
19+
}
20+
}
21+
result[i] = j - i;
22+
}
23+
return result;
24+
}
25+
}

0 commit comments

Comments
 (0)