-
Notifications
You must be signed in to change notification settings - Fork 481
/
1255.go
42 lines (37 loc) · 803 Bytes
/
1255.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
var n int
var cnt []int
func maxScoreWords(words []string, letters []byte, score []int) int {
cnt = make([]int, 26)
n = len(words)
for _, c := range letters {
cnt[int(c) - 97]++
}
return dfs(0, words, score)
}
func dfs(i int, words []string, score []int) int {
if i == n {
return 0
}
res, tmp, val := max(0, dfs(i + 1, words, score)), 0, 1
for _, c := range words[i] {
t := int(c) - 97
cnt[t]--
tmp += score[t]
if cnt[t] < 0 {
val = 0
}
}
if val == 1 {
res = max(res, dfs(i + 1, words, score) + tmp)
}
for _, c := range words[i] {
cnt[int(c) - 97]++
}
return res
}
func max(a, b int) int {
if a > b {
return a
}
return b
}