-
Notifications
You must be signed in to change notification settings - Fork 4
/
1301. Number of Paths with Max Score.go
76 lines (73 loc) · 1.48 KB
/
1301. Number of Paths with Max Score.go
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
package main
import "math"
func pathsWithMaxScore(board []string) []int {
mod := int(math.Pow10(9)) + 7
m := len(board)
max := make([][]int, m, m)
for i := range max {
max[i] = make([]int, m, m)
}
cnt := make([][]int, m, m)
for i := range cnt {
cnt[i] = make([]int, m, m)
}
max[0][0] = 0
cnt[0][0] = 1
for i := 1; i < m; i++ {
if board[i][0] == 'X' || max[i-1][0] == -1 {
max[i][0] = -1
cnt[i][0] = 0
} else {
max[i][0] = max[i-1][0] + int(board[i][0]-'0')
cnt[i][0] = 1
}
}
for i := 1; i < m; i++ {
if board[0][i] == 'X' || max[0][i-1] == -1 {
max[0][i] = -1
cnt[0][i] = 0
} else {
max[0][i] = max[0][i-1] + int(board[0][i]-'0')
cnt[0][i] = 1
}
}
for i := 1; i < m; i++ {
for j := 1; j < m; j++ {
if board[i][j] == 'X' {
max[i][j] = -1
cnt[i][j] = 0
} else {
prevMax := getMax(max[i-1][j-1], getMax(max[i-1][j], max[i][j-1]))
if prevMax == -1 {
max[i][j] = -1
cnt[i][j] = 0
} else {
max[i][j] = prevMax + int(board[i][j]-'0')
if prevMax == max[i-1][j] {
cnt[i][j] += cnt[i-1][j]
cnt[i][j] %= mod
}
if prevMax == max[i-1][j-1] {
cnt[i][j] += cnt[i-1][j-1]
cnt[i][j] %= mod
}
if prevMax == max[i][j-1] {
cnt[i][j] += cnt[i][j-1]
cnt[i][j] %= mod
}
}
}
}
}
if max[m-1][m-1] == -1 {
return []int{0, 0}
} else {
return []int{max[m-1][m-1] - int('S'-'0'), cnt[m-1][m-1]}
}
}
func getMax(a, b int) int {
if a > b {
return a
}
return b
}