作者ray90514 ()
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Fri May 24 11:16:20 2024
1255. Maximum Score Words Formed by Letters
今天是hard
紀錄所有可能的組合算一個最大值 重複的跳過
不過一開始寫成flag = 害我卡住不知道哪裡錯==
class Solution {
public:
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>&
score) {
unordered_set<string> st;
vector<pair<string, int>> dp;
string abc(26, 0);
for(char c : letters){
abc[c - 'a']++;
}
dp.push_back({string(26, 0), 0});
int ans = 0;
string s(26, 0);
for(int i = 0; i < words.size(); i++){
for(int j = dp.size() - 1; j >= 0; j--){
bool flag = false;
pair<string, int> p = dp[j];
for(char c : words[i]){
p.first[c - 'a']++;
flag |= p.first[c - 'a'] > abc[c - 'a'];
p.second += score[c - 'a'];
}
if(flag)
continue;
if(!st.count(p.first)){
dp.push_back(p);
st.emplace(p.first);
if(p.second > ans){
ans = p.second;
s = p.first;
}
}
}
}
return ans;
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.36.41.116 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1716520582.A.F21.html
推 DJYOSHITAKA: 大濕... 05/24 11:17
推 sustainer123: 大師 05/24 11:22
→ yam276: 我獨自刷題 05/24 11:23
推 orangeNoob: 大師 05/24 12:47
推 family2909: 別卷了 05/24 13:32
推 sixB: 大師 05/24 15:14