Add solution 1631、1691

This commit is contained in:
YDZ
2021-02-13 22:34:09 +08:00
parent c7862ae2bd
commit af41c91d60
24 changed files with 903 additions and 76 deletions

View File

@ -0,0 +1,108 @@
package leetcode
import (
"sort"
"github.com/halfrost/LeetCode-Go/template"
)
var dir = [4][2]int{
{0, 1},
{1, 0},
{0, -1},
{-1, 0},
}
// 解法一 DFS + 二分
func minimumEffortPath(heights [][]int) int {
n, m := len(heights), len(heights[0])
visited := make([][]bool, n)
for i := range visited {
visited[i] = make([]bool, m)
}
low, high := 0, 1000000
for low < high {
threshold := low + (high-low)>>1
if !hasPath(heights, visited, 0, 0, threshold) {
low = threshold + 1
} else {
high = threshold
}
for i := range visited {
for j := range visited[i] {
visited[i][j] = false
}
}
}
return low
}
func hasPath(heights [][]int, visited [][]bool, i, j, threshold int) bool {
n, m := len(heights), len(heights[0])
if i == n-1 && j == m-1 {
return true
}
visited[i][j] = true
res := false
for _, d := range dir {
ni, nj := i+d[0], j+d[1]
if ni < 0 || ni >= n || nj < 0 || nj >= m || visited[ni][nj] || res {
continue
}
diff := abs(heights[i][j] - heights[ni][nj])
if diff <= threshold && hasPath(heights, visited, ni, nj, threshold) {
res = true
}
}
return res
}
func abs(a int) int {
if a < 0 {
a = -a
}
return a
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
// 解法二 并查集
func minimumEffortPath1(heights [][]int) int {
n, m, edges, uf := len(heights), len(heights[0]), []edge{}, template.UnionFind{}
uf.Init(n * m)
for i, row := range heights {
for j, h := range row {
id := i*m + j
if i > 0 {
edges = append(edges, edge{id - m, id, abs(h - heights[i-1][j])})
}
if j > 0 {
edges = append(edges, edge{id - 1, id, abs(h - heights[i][j-1])})
}
}
}
sort.Slice(edges, func(i, j int) bool { return edges[i].diff < edges[j].diff })
for _, e := range edges {
uf.Union(e.v, e.w)
if uf.Find(0) == uf.Find(n*m-1) {
return e.diff
}
}
return 0
}
type edge struct {
v, w, diff int
}

View File

@ -0,0 +1,52 @@
package leetcode
import (
"fmt"
"testing"
)
type question1631 struct {
para1631
ans1631
}
// para 是参数
// one 代表第一个参数
type para1631 struct {
heights [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1631 struct {
one int
}
func Test_Problem1631(t *testing.T) {
qs := []question1631{
{
para1631{[][]int{{1, 2, 2}, {3, 8, 2}, {5, 3, 5}}},
ans1631{2},
},
{
para1631{[][]int{{1, 2, 3}, {3, 8, 4}, {5, 3, 5}}},
ans1631{1},
},
{
para1631{[][]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}}},
ans1631{0},
},
}
fmt.Printf("------------------------Leetcode Problem 1631------------------------\n")
for _, q := range qs {
_, p := q.ans1631, q.para1631
fmt.Printf("【input】:%v 【output】:%v \n", p, minimumEffortPath(p.heights))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,170 @@
# [1631. Path With Minimum Effort](https://leetcode.com/problems/path-with-minimum-effort/)
## 题目
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:**
![https://assets.leetcode.com/uploads/2020/10/04/ex1.png](https://assets.leetcode.com/uploads/2020/10/04/ex1.png)
```
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:**
![https://assets.leetcode.com/uploads/2020/10/04/ex2.png](https://assets.leetcode.com/uploads/2020/10/04/ex2.png)
```
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:**
![https://assets.leetcode.com/uploads/2020/10/04/ex3.png](https://assets.leetcode.com/uploads/2020/10/04/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.
```
**Constraints:**
- `rows == heights.length`
- `columns == heights[i].length`
- `1 <= rows, columns <= 100`
- `1 <= heights[i][j] <= 10^6`
## 题目大意
你准备参加一场远足活动。给你一个二维 `rows x columns` 的地图 `heights` ,其中 `heights[row][col]` 表示格子 `(row, col)` 的高度。一开始你在最左上角的格子 `(0, 0)` ,且你希望去最右下角的格子 `(rows-1, columns-1)` (注意下标从 0 开始编号)。你每次可以往 上,下,左,右 四个方向之一移动,你想要找到耗费 体力 最小的一条路径。一条路径耗费的 体力值 是路径上相邻格子之间 高度差绝对值 的 最大值 决定的。请你返回从左上角走到右下角的最小 体力消耗值 。
## 解题思路
- 此题和第 778 题解题思路完全一致。在第 778 题中求的是最短连通时间。此题求的是连通路径下的最小体力值。都是求的最小值,只是 2 个值的意义不同罢了。
- 按照第 778 题的思路,本题也有多种解法。第一种解法是 DFS + 二分。先将题目变换一个等价问法。题目要求找到最小体力消耗值,也相当于问是否存在一个体力消耗值 x只要大于等于 x一定能连通。利用二分搜索来找到这个临界值。体力消耗值是有序的此处满足二分搜索的条件。题目给定柱子高度是 [1,10^6],所以体力值一定在 [0,10^6-1] 这个区间内。判断是否取中值的条件是用 DFS 或者 BFS 搜索 (0,0) 点和 (N-1, N-1) 点之间是否连通。时间复杂度O(mnlogC),其中 m 和 n 分别是地图的行数和列数C 是格子的最大高度。C 最大为 10^6所以 logC 常数也很小。空间复杂度 O(mn)。
- 第二种解法是并查集。将图中所有边按照权值从小到大进行排序,并依次加入并查集中。直到加入一条权值为 x 的边以后,左上角到右下角连通了。最小体力消耗值也就找到了。注意加入边的时候,只加入 `i-1``i` `j-1``j` 这 2 类相邻的边。因为最小体力消耗意味着不走回头路。上下左右四个方向到达一个节点只可能从上边和左边走过来。从下边和右边走过来肯定是浪费体力了。时间复杂度O(mnlog(mn)),其中 m 和 n 分别是地图的行数和列数,图中的边数为 O(mn)。空间复杂度 O(mn),即为存储所有边以及并查集需要的空间。
## 代码
```go
package leetcode
import (
"sort"
"github.com/halfrost/LeetCode-Go/template"
)
var dir = [4][2]int{
{0, 1},
{1, 0},
{0, -1},
{-1, 0},
}
// 解法一 DFS + 二分
func minimumEffortPath(heights [][]int) int {
n, m := len(heights), len(heights[0])
visited := make([][]bool, n)
for i := range visited {
visited[i] = make([]bool, m)
}
low, high := 0, 1000000
for low < high {
threshold := low + (high-low)>>1
if !hasPath(heights, visited, 0, 0, threshold) {
low = threshold + 1
} else {
high = threshold
}
for i := range visited {
for j := range visited[i] {
visited[i][j] = false
}
}
}
return low
}
func hasPath(heights [][]int, visited [][]bool, i, j, threshold int) bool {
n, m := len(heights), len(heights[0])
if i == n-1 && j == m-1 {
return true
}
visited[i][j] = true
res := false
for _, d := range dir {
ni, nj := i+d[0], j+d[1]
if ni < 0 || ni >= n || nj < 0 || nj >= m || visited[ni][nj] || res {
continue
}
diff := abs(heights[i][j] - heights[ni][nj])
if diff <= threshold && hasPath(heights, visited, ni, nj, threshold) {
res = true
}
}
return res
}
func abs(a int) int {
if a < 0 {
a = -a
}
return a
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a < b {
return b
}
return a
}
// 解法二 并查集
func minimumEffortPath1(heights [][]int) int {
n, m, edges, uf := len(heights), len(heights[0]), []edge{}, template.UnionFind{}
uf.Init(n * m)
for i, row := range heights {
for j, h := range row {
id := i*m + j
if i > 0 {
edges = append(edges, edge{id - m, id, abs(h - heights[i-1][j])})
}
if j > 0 {
edges = append(edges, edge{id - 1, id, abs(h - heights[i][j-1])})
}
}
}
sort.Slice(edges, func(i, j int) bool { return edges[i].diff < edges[j].diff })
for _, e := range edges {
uf.Union(e.v, e.w)
if uf.Find(0) == uf.Find(n*m-1) {
return e.diff
}
}
return 0
}
type edge struct {
v, w, diff int
}
```

View File

@ -0,0 +1,42 @@
package leetcode
import "sort"
func maxHeight(cuboids [][]int) int {
n := len(cuboids)
for i := 0; i < n; i++ {
sort.Ints(cuboids[i]) // 立方体三边内部排序
}
// 立方体排序,先按最短边,再到最长边
sort.Slice(cuboids, func(i, j int) bool {
if cuboids[i][0] != cuboids[j][0] {
return cuboids[i][0] < cuboids[j][0]
}
if cuboids[i][1] != cuboids[j][1] {
return cuboids[i][1] < cuboids[j][1]
}
return cuboids[i][2] < cuboids[j][2]
})
res := 0
dp := make([]int, n)
for i := 0; i < n; i++ {
dp[i] = cuboids[i][2]
res = max(res, dp[i])
}
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if cuboids[j][0] <= cuboids[i][0] && cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
dp[i] = max(dp[i], dp[j]+cuboids[i][2])
}
}
res = max(res, dp[i])
}
return res
}
func max(x, y int) int {
if x > y {
return x
}
return y
}

View File

@ -0,0 +1,52 @@
package leetcode
import (
"fmt"
"testing"
)
type question1691 struct {
para1691
ans1691
}
// para 是参数
// one 代表第一个参数
type para1691 struct {
cuboids [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1691 struct {
one int
}
func Test_Problem1691(t *testing.T) {
qs := []question1691{
{
para1691{[][]int{{50, 45, 20}, {95, 37, 53}, {45, 23, 12}}},
ans1691{190},
},
{
para1691{[][]int{{38, 25, 45}, {76, 35, 3}}},
ans1691{76},
},
{
para1691{[][]int{{7, 11, 17}, {7, 17, 11}, {11, 7, 17}, {11, 17, 7}, {17, 7, 11}, {17, 11, 7}}},
ans1691{102},
},
}
fmt.Printf("------------------------Leetcode Problem 1691------------------------\n")
for _, q := range qs {
_, p := q.ans1691, q.para1691
fmt.Printf("【input】:%v 【output】:%v\n", p, maxHeight(p.cuboids))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,106 @@
# [1691. Maximum Height by Stacking Cuboids](https://leetcode.com/problems/maximum-height-by-stacking-cuboids/)
## 题目
Given `n` `cuboids` where the dimensions of the `ith` cuboid is `cuboids[i] = [widthi, lengthi, heighti]` (**0-indexed**). Choose a **subset** of `cuboids` and place them on each other.
You can place cuboid `i` on cuboid `j` if `widthi <= widthj` and `lengthi <= lengthj` and `heighti <= heightj`. You can rearrange any cuboid's dimensions by rotating it to put it on another cuboid.
Return *the **maximum height** of the stacked* `cuboids`.
**Example 1:**
![https://assets.leetcode.com/uploads/2019/10/21/image.jpg](https://assets.leetcode.com/uploads/2019/10/21/image.jpg)
```
Input: cuboids = [[50,45,20],[95,37,53],[45,23,12]]
Output: 190
Explanation:
Cuboid 1 is placed on the bottom with the 53x37 side facing down with height 95.
Cuboid 0 is placed next with the 45x20 side facing down with height 50.
Cuboid 2 is placed next with the 23x12 side facing down with height 45.
The total height is 95 + 50 + 45 = 190.
```
**Example 2:**
```
Input: cuboids = [[38,25,45],[76,35,3]]
Output: 76
Explanation:
You can't place any of the cuboids on the other.
We choose cuboid 1 and rotate it so that the 35x3 side is facing down and its height is 76.
```
**Example 3:**
```
Input: cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]
Output: 102
Explanation:
After rearranging the cuboids, you can see that all cuboids have the same dimension.
You can place the 11x7 side down on all cuboids so their heights are 17.
The maximum height of stacked cuboids is 6 * 17 = 102.
```
**Constraints:**
- `n == cuboids.length`
- `1 <= n <= 100`
- `1 <= widthi, lengthi, heighti <= 100`
## 题目大意
给你 n 个长方体 cuboids ,其中第 i 个长方体的长宽高表示为 cuboids[i] = [widthi, lengthi, heighti](下标从 0 开始)。请你从 cuboids 选出一个 子集 ,并将它们堆叠起来。如果 widthi <= widthj 且 lengthi <= lengthj 且 heighti <= heightj ,你就可以将长方体 i 堆叠在长方体 j 上。你可以通过旋转把长方体的长宽高重新排列,以将它放在另一个长方体上。返回 堆叠长方体 cuboids 可以得到的 最大高度 。
## 解题思路
- 这一题是 LIS 最长递增子序列系列问题的延续。一维 LIS 问题是第 300 题。二维 LIS 问题是 354 题。这一题是三维的 LIS 问题。
- 题目要求最终摞起来的长方体尽可能的高,那么把长宽高中最大的值旋转为高。这是针对单个方块的排序。多个方块间还要排序,因为他们摞起来有要求,大的方块必须放在下面。所以针对多个方块,按照长,宽,高的顺序进行排序。两次排序完成以后,可以用动态规划找出最大值了。定义 `dp[i]` 为以 `i` 为最后一块砖块所能堆叠的最高高度。由于长和宽已经排好序了。所以只需要在 [0, i - 1] 这个区间内动态更新 dp 最大值。
## 代码
```go
package leetcode
import "sort"
func maxHeight(cuboids [][]int) int {
n := len(cuboids)
for i := 0; i < n; i++ {
sort.Ints(cuboids[i]) // 立方体三边内部排序
}
// 立方体排序,先按最短边,再到最长边
sort.Slice(cuboids, func(i, j int) bool {
if cuboids[i][0] != cuboids[j][0] {
return cuboids[i][0] < cuboids[j][0]
}
if cuboids[i][1] != cuboids[j][1] {
return cuboids[i][1] < cuboids[j][1]
}
return cuboids[i][2] < cuboids[j][2]
})
res := 0
dp := make([]int, n)
for i := 0; i < n; i++ {
dp[i] = cuboids[i][2]
res = max(res, dp[i])
}
for i := 1; i < n; i++ {
for j := 0; j < i; j++ {
if cuboids[j][0] <= cuboids[i][0] && cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2] {
dp[i] = max(dp[i], dp[j]+cuboids[i][2])
}
}
res = max(res, dp[i])
}
return res
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
```