Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1631 #624

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions leetcode/1601-1700/1631.Path-With-Minimum-Effort/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
# [1631.Path With Minimum Effort][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
You are a hiker preparing for an upcoming hike. You are given `heights`, a 2D array of size `rows x columns`, where `heights[row][col]` represents the height of cell (`row, col`). You are situated in the top-left cell, (`0, 0`), and you hope to travel to the bottom-right cell, (`rows-1, columns-1`) (i.e., **0-indexed**). You can move **up**, **down**, **left**, or **right**, and you wish to find a route that requires the minimum **effort**.

A route's **effort** is the **maximum absolute difference** in heights between two consecutive cells of the route.

Return the minimum **effort** required to travel from the top-left cell to the bottom-right cell.

**Example 1:**
**Example 1:**

![example1](./ex1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: heights = [[1,2,2],[3,8,2],[5,3,5]]
Output: 2
Explanation: The route of [1,3,5,3,5] has a maximum absolute difference of 2 in consecutive cells.
This is better than the route of [1,2,2,2,5], where the maximum absolute difference is 3.
```

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

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

### 思路1
> ...
Path With Minimum Effort
```go
```
Input: heights = [[1,2,3],[3,8,4],[5,3,5]]
Output: 1
Explanation: The route of [1,2,3,4,5] has a maximum absolute difference of 1 in consecutive cells, which is better than route [1,3,5,3,5].
```

**Example 3:**

![example3](./ex3.png)

```
Input: heights = [[1,2,1,1,1],[1,2,1,2,1],[1,2,1,2,1],[1,2,1,2,1],[1,1,1,2,1]]
Output: 0
Explanation: This route does not require any effort.
```

## 结语

Expand Down
52 changes: 50 additions & 2 deletions leetcode/1601-1700/1631.Path-With-Minimum-Effort/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(heights [][]int) int {
// binary search + bfs
var (
left = 0
right = 1000000
bfs func(int, int, int) bool
dirs = [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}
)

rows, cols := len(heights), len(heights[0])
bfs = func(x, y, limit int) bool {
visited := [100][100]bool{}
queue := [][]int{{x, y}}
visited[x][y] = true
for len(queue) > 0 {
next := make([][]int, 0)
for _, item := range queue {
if item[0] == rows-1 && item[1] == cols-1 {
return true
}
for _, dir := range dirs {
nx, ny := item[0]+dir[0], item[1]+dir[1]
if nx < 0 || nx >= rows || ny < 0 || ny >= cols || visited[nx][ny] {
continue
}
diff := heights[nx][ny] - heights[item[0]][item[1]]
if diff < 0 {
diff = ^diff + 1
}
if diff > limit {
continue
}
next = append(next, []int{nx, ny})
visited[nx][ny] = true
}
}
queue = next
}
return false
}
for left < right {
m := left + (right-left)/2
if bfs(0, 0, m) {
right = m
continue
}
left = m + 1
}

return left
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{1, 2, 2}, {3, 8, 2}, {5, 3, 5}}, 2},
{"TestCase2", [][]int{{1, 2, 3}, {3, 8, 4}, {5, 3, 5}}, 1},
{"TestCase3", [][]int{{1, 2, 1, 1, 1}, {1, 2, 1, 2, 1}, {1, 2, 1, 2, 1}, {1, 2, 1, 2, 1}, {1, 1, 1, 2, 1}}, 0},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

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

// 使用案列
// 使用案列
func ExampleSolution() {
}
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.
Loading