-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1102-PathWithMaximumMinimumValue.go
More file actions
172 lines (158 loc) · 5.04 KB
/
1102-PathWithMaximumMinimumValue.go
File metadata and controls
172 lines (158 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
// 1102. Path With Maximum Minimum Value
// Given an m x n integer matrix grid,
// return the maximum score of a path starting at (0, 0) and ending at (m - 1, n - 1) moving in the 4 cardinal directions.
// The score of a path is the minimum value in that path.
// For example, the score of the path 8 → 4 → 5 → 9 is 4.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/08/05/maxgrid1.jpg" />
// Input: grid = [[5,4,5],[1,2,6],[7,4,6]]
// Output: 4
// Explanation: The path with the maximum score is highlighted in yellow.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2021/08/05/maxgrid2.jpg" />
// Input: grid = [[2,2,1,2,2,2],[1,2,2,2,1,2]]
// Output: 2
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2021/08/05/maxgrid3.jpg" />
// Input: grid = [[3,4,6,3,4],[0,2,1,1,7],[8,8,3,2,7],[3,2,4,9,8],[4,1,2,0,0],[4,6,5,4,3]]
// Output: 3
// Constraints:
// m == grid.length
// n == grid[i].length
// 1 <= m, n <= 100
// 0 <= grid[i][j] <= 10^9
import "fmt"
import "container/heap"
type Cell struct {
id int
score int
}
type CellPQ []*Cell
func (p CellPQ) Len() int { return len(p) }
func (p CellPQ) Less(i, j int) bool {
return p[i].score > p[j].score
}
func (p CellPQ) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p *CellPQ) Push(x interface{}) {
(*p) = append(*p, x.(*Cell))
}
func (p *CellPQ) Pop() interface{} {
n := p.Len()
item := (*p)[n-1]
*p = (*p)[:n-1]
return item
}
func maximumMinimumPath(grid [][]int) int {
min := func (x, y int) int { if x < y { return x; }; return y; }
q := &CellPQ{}
heap.Init(q)
tr, tc := len(grid), len(grid[0])
visited := make(map[int]bool)
heap.Push(q, &Cell{0, grid[0][0]})
visited[0] = true
res := min(grid[0][0], grid[tr - 1][tc - 1])
for q.Len() > 0 {
cc := heap.Pop(q).(*Cell)
res = min(res, cc.score)
crow, ccol := cc.id/tc, cc.id%tc
// 4 directions explore, adding not visited nodes
if crow > 0 {
nid := (crow-1)*tc + ccol
if _, ok := visited[nid]; !ok {
heap.Push(q, &Cell{nid, grid[crow-1][ccol]})
visited[nid] = true
}
}
if ccol > 0 {
nid := crow*tc + ccol - 1
if _, ok := visited[nid]; !ok {
heap.Push(q, &Cell{nid, grid[crow][ccol-1]})
visited[nid] = true
}
}
if crow < len(grid)-1 {
nid := (crow+1)*tc + ccol
if _, ok := visited[nid]; !ok {
if nid == tr*tc-1 { // 到终点了
break
}
heap.Push(q, &Cell{nid, grid[crow+1][ccol]})
visited[nid] = true
}
}
if ccol < tc-1 {
nid := crow*tc + ccol + 1
if _, ok := visited[nid]; !ok {
if nid == tr*tc-1 { // 到终点了
break
}
heap.Push(q, &Cell{nid, grid[crow][ccol+1]})
visited[nid] = true
}
}
}
return res
}
func maximumMinimumPath1(grid [][]int) int {
var visited [101][101]bool
dirs := [][2]int{{1, 0}, {0, 1}, {-1, 0}, {0, -1}} // 4个方向
row, col := len(grid), len(grid[0])
min := func (x, y int) int { if x < y { return x; }; return y; }
left, right := 0, min(grid[0][0],grid[row - 1][col - 1])
var pathExists func(val, curRow, curCol int) bool
pathExists = func(val, curRow, curCol int) bool {
if curRow == row - 1 && curCol == col - 1 {
return true
}
visited[curRow][curCol] = true
for _, dir := range dirs {
newRow, newCol := curRow + dir[0], curCol + dir[1]
if newRow >= 0 && newRow < row && newCol >= 0 && newCol < col &&
!visited[newRow][newCol] && grid[newRow][newCol] >= val {
if pathExists(val, newRow, newCol) {
return true
}
}
}
return false
}
for left < right {
middle := (left + right + 1) >> 1
for i := 0; i < row; i++ {
for j := 0; j < col; j++ {
visited[i][j] = false
}
}
if pathExists(middle, 0, 0) {
left = middle
} else {
right = middle - 1
}
}
return left
}
func main() {
fmt.Println(maximumMinimumPath([][]int{{5,4,5},{1,2,6},{7,4,6}})) // 4
fmt.Println(maximumMinimumPath([][]int{{2,2,1,2,2,2},{1,2,2,2,1,2}})) // 2
fmt.Println(maximumMinimumPath([][]int{
{3,4,6,3,4},
{0,2,1,1,7},
{8,8,3,2,7},
{3,2,4,9,8},
{4,1,2,0,0},
{4,6,5,4,3},
})) // 3
fmt.Println(maximumMinimumPath1([][]int{{5,4,5},{1,2,6},{7,4,6}})) // 4
fmt.Println(maximumMinimumPath1([][]int{{2,2,1,2,2,2},{1,2,2,2,1,2}})) // 2
fmt.Println(maximumMinimumPath1([][]int{
{3,4,6,3,4},
{0,2,1,1,7},
{8,8,3,2,7},
{3,2,4,9,8},
{4,1,2,0,0},
{4,6,5,4,3},
})) // 3
}