mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2026-03-13 10:02:05 +08:00
添加 problem 778
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package leetcode
|
||||
|
||||
// 解法一 DFS + 二分
|
||||
func swimInWater(grid [][]int) int {
|
||||
row, col, flags, minWait, maxWait := len(grid), len(grid[0]), make([][]int, len(grid)), 0, 0
|
||||
for i, row := range grid {
|
||||
flags[i] = make([]int, len(row))
|
||||
for j := 0; j < col; j++ {
|
||||
flags[i][j] = -1
|
||||
if row[j] > maxWait {
|
||||
maxWait = row[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
for minWait < maxWait {
|
||||
midWait := (minWait + maxWait) / 2
|
||||
addFlags(grid, flags, midWait, 0, 0)
|
||||
if flags[row-1][col-1] == midWait {
|
||||
maxWait = midWait
|
||||
} else {
|
||||
minWait = midWait + 1
|
||||
}
|
||||
}
|
||||
return minWait
|
||||
}
|
||||
|
||||
func addFlags(grid [][]int, flags [][]int, flag int, row int, col int) {
|
||||
if row < 0 || col < 0 || row >= len(grid) || col >= len(grid[0]) {
|
||||
return
|
||||
}
|
||||
if grid[row][col] > flag || flags[row][col] == flag {
|
||||
return
|
||||
}
|
||||
flags[row][col] = flag
|
||||
addFlags(grid, flags, flag, row-1, col)
|
||||
addFlags(grid, flags, flag, row+1, col)
|
||||
addFlags(grid, flags, flag, row, col-1)
|
||||
addFlags(grid, flags, flag, row, col+1)
|
||||
}
|
||||
|
||||
// 解法二 并查集(并不是此题的最优解)
|
||||
func swimInWater1(grid [][]int) int {
|
||||
n, uf, res := len(grid), UnionFind{}, 0
|
||||
uf.init(n * n)
|
||||
for uf.find(0) != uf.find(n*n-1) {
|
||||
for i := 0; i < n; i++ {
|
||||
for j := 0; j < n; j++ {
|
||||
if grid[i][j] > res {
|
||||
continue
|
||||
}
|
||||
if i < n-1 && grid[i+1][j] <= res {
|
||||
uf.union(i*n+j, i*n+j+n)
|
||||
}
|
||||
if j < n-1 && grid[i][j+1] <= res {
|
||||
uf.union(i*n+j, i*n+j+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
res++
|
||||
}
|
||||
return res - 1
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question778 struct {
|
||||
para778
|
||||
ans778
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para778 struct {
|
||||
grid [][]int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans778 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem778(t *testing.T) {
|
||||
|
||||
qs := []question778{
|
||||
question778{
|
||||
para778{[][]int{[]int{0, 2}, []int{1, 3}}},
|
||||
ans778{3},
|
||||
},
|
||||
|
||||
question778{
|
||||
para778{[][]int{[]int{0, 1, 2, 3, 4}, []int{24, 23, 22, 21, 5}, []int{12, 13, 14, 15, 16}, []int{11, 17, 18, 19, 20}, []int{10, 9, 8, 7, 6}}},
|
||||
ans778{16},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 778------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans778, q.para778
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, swimInWater(p.grid))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
||||
Reference in New Issue
Block a user