forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1255.cpp
30 lines (29 loc) · 792 Bytes
/
1255.cpp
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
class Solution
{
public:
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score)
{
n = words.size();
cnt = vector<int>(26, 0);
for (char c : letters) cnt[c - 97]++;
return dfs(0, words, score);
}
private:
vector<int> cnt;
int n;
int dfs(int i, vector<string>& words, vector<int>& score)
{
if (i == n) return 0;
int res = max(0, dfs(i + 1, words, score)), tmp = 0, val = 1;
for (char c : words[i])
{
int t = c - 97;
cnt[t]--;
tmp += score[t];
if (cnt[t] < 0) val = 0;
}
if (val) res = max(res, dfs(i + 1, words, score) + tmp);
for (char c : words[i]) cnt[c - 97]++;
return res;
}
};