Skip to content

Commit

Permalink
Merge pull request #656 from 0xff-dev/2120
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 2120
  • Loading branch information
6boris authored Oct 28, 2023
2 parents fbb3ae6 + 0bf2954 commit df010f5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 25 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
# [2120.Execution of All Suffix Instructions Staying in a Grid][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
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 (start<sub>row</sub>, start<sub>col</sub>).

You are also given a **0-indexed** string `s` of length `m` where `s[i]` is the i<sup>th</sup> instruction for the robot: `'L'` (move left), `'R'` (move right), `'U'` (move up), and `'D'` (move down).

The robot can begin executing from any i<sup>th</sup> 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:

- The next instruction will move the robot off the grid.
- There are no more instructions left to execute.

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<sup>th</sup> instruction in `s`.

**Example 1:**

**Example 1:**
![example1](./1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: n = 3, startPos = [0,1], s = "RRDDLU"
Output: [1,5,4,3,1,0]
Explanation: Starting from startPos and beginning execution from the ith instruction:
- 0th: "RRDDLU". Only one instruction "R" can be executed before it moves off the grid.
- 1st: "RDDLU". All five instructions can be executed while it stays in the grid and ends at (1, 1).
- 2nd: "DDLU". All four instructions can be executed while it stays in the grid and ends at (1, 0).
- 3rd: "DLU". All three instructions can be executed while it stays in the grid and ends at (0, 0).
- 4th: "LU". Only one instruction "L" can be executed before it moves off the grid.
- 5th: "U". If moving up, it would move off the grid.
```

## 题意
> ...
**Example 2:**

## 题解
![example2](./2.png)

### 思路1
> ...
Execution of All Suffix Instructions Staying in a Grid
```go
```
Input: n = 2, startPos = [1,1], s = "LURD"
Output: [4,1,0,0]
Explanation:
- 0th: "LURD".
- 1st: "URD".
- 2nd: "RD".
- 3rd: "D".
```

**Example 3:**

![example3](./3.png)

```
Input: n = 1, startPos = [0,0], s = "LRUD"
Output: [0,0,0,0]
Explanation: No matter which instruction the robot begins execution from, it would move off the grid.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(n int, startPos []int, s string) []int {
ans := make([]int, len(s))
var steps func(int, int, int) int

steps = func(x, y, index int) int {
ans := 0
for i := index; i < len(s); i++ {
a, b := 0, 0
if s[i] == 'L' {
a, b = 0, -1
} else if s[i] == 'R' {
a, b = 0, 1
} else if s[i] == 'U' {
a, b = -1, 0
} else {
a, b = 1, 0
}
nx, ny := x+a, y+b
if nx < 0 || nx >= n || ny < 0 || ny >= n {
return ans
}
x, y = nx, ny
ans++
}
return ans
}
for i := 0; i < len(s); i++ {
ans[i] = steps(startPos[0], startPos[1], i)
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n int
pos []int
s string
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 3, []int{0, 1}, "RRDDLU", []int{1, 5, 4, 3, 1, 0}},
{"TestCase2", 2, []int{1, 1}, "LURD", []int{4, 1, 0, 0}},
{"TestCase3", 1, []int{0, 0}, "LRUD", []int{0, 0, 0, 0}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n, c.pos, c.s)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.n, c.pos, c.s)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit df010f5

Please sign in to comment.