【leetcode】1255. Maximum Score Words Formed by Letters
题目如下:
Given a list of
words
, list of singleletters
(might be repeating) andscore
of every character.Return the maximum score of any valid set of words formed by using the given letters (
words[i]
cannot be used two or more times).It is not necessary to use all characters in
letters
and each letter can only be used once. Score of letters'a'
,'b'
,'c'
, ... ,'z'
is given byscore[0]
,score[1]
, ... ,score[25]
respectively.Example 1:
Input: words = ["dog","cat","dad","good"], letters = ["a","a","c","d","d","d","g","o","o"],
score = [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0]
Output: 23
Explanation:
Score a=1, c=9, d=5, g=3, o=2
Given letters, we can form the words "dad" (5+1+5) and "good" (3+2+2+5) with a score of 23.
Words "dad" and "dog" only get a score of 21.Example 2:
Input: words = ["xxxz","ax","bx","cx"], letters = ["z","a","b","c","x","x","x"],
score = [4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,10]
Output: 27
Explanation:
Score a=4, b=4, c=4, x=5, z=10
Given letters, we can form the words "ax" (4+5), "bx" (4+5) and "cx" (4+5) with a score of 27.
Word "xxxz" only get a score of 25.Example 3:
Input: words = ["leetcode"], letters = ["l","e","t","c","o","d"], score = [0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0]
Output: 0
Explanation:
Letter "e" can only be used once.Constraints:
1 <= words.length <= 14
1 <= words[i].length <= 15
1 <= letters.length <= 100
letters[i].length == 1
score.length == 26
0 <= score[i] <= 10
words[i]
,letters[i]
contains only lower case English letters.
解题思路:这题也能算hard级别?words.length 最大才14,那么总共有2^14次方种组合,全列举出来求最大值即可。
代码如下:
class Solution(object):
def maxScoreWords(self, words, letters, score):
"""
:type words: List[str]
:type letters: List[str]
:type score: List[int]
:rtype: int
"""
def checkValid(string,dic):
for i in string:
if i not in dic or string.count(i) > dic[i]:
return False
return True
def calc(string,dic):
uniq = set(list(string))
count = 0
for i in uniq:
count += string.count(i) * score[ord(i) - ord('a')]
return count dic = {}
for i in letters:
dic[i] = dic.setdefault(i,0) + 1
queue = []
for i in range(len(words)):
if checkValid(words[i],dic):
queue.append((i,words[i]))
res = 0
while len(queue) > 0:
inx,word = queue.pop(0)
res = max(res,calc(word,dic))
for i in range(inx+1,len(words)):
if checkValid(word + words[i],dic):
queue.append((i,word + words[i]))
return res
【leetcode】1255. Maximum Score Words Formed by Letters的更多相关文章
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- LeetCode 1255 得分最高的单词集合 Maximum Score Words Formed by Letters
地址 https://leetcode-cn.com/problems/maximum-score-words-formed-by-letters/ 题目描述你将会得到一份单词表 words,一个字母 ...
- 【leetcode】1189. Maximum Number of Balloons
题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
随机推荐
- java application.properties 密码加密
main方法 public static void main(String[] args) { BasicTextEncryptor textEncryptor = new BasicTextEncr ...
- powerdesiner概念模型转物理模型以及失败的解决方法
powerdesiner概念模型转物理模型 概念模型转物理模型 关于powerdesinger 出现Entity Attribute code uniqueness解决办法 Entity Attrib ...
- 【pytorch】学习笔记(三)-激励函数
[pytorch]学习笔记-激励函数 学习自:莫烦python 什么是激励函数 一句话概括 Activation: 就是让神经网络可以描述非线性问题的步骤, 是神经网络变得更强大 1.激活函数是用来加 ...
- CSUST 2012 一个顶俩 (本校OJ题)(思维+树链剖分)
(点击这里查看原题,不保证可以进去....外网可能比较卡) Description A:一心一意 B:一个顶俩 最近QQ更新后那个成语接龙好像挺火的?但我只知道图论里一条边是一个顶俩个点的emm. 如 ...
- # 组合数学-组合数+lacus定理
目录 数论-组合数+lacus定理 组合数计算 lacus定理-大组合数取模 数论-组合数+lacus定理 组合数计算 为避免爆long long,\(20!\)就达到了long long 的范围,采 ...
- BugkuCTF--域名解析(windows)
这是这道题的题目,很简洁,flag获得的方法也告诉你了,就差把域名解析. 那么域名怎么解析呢.. 打开C:\Windows\System32\drivers\etc中的hosts文件(用记事本打开), ...
- redis 学习(6)-- 集合类型
redis 学习(6)-- 集合类型 set 结构 无序 无重复 集合间操作 set 集合内操作 命令 含义 sadd key memebr1 [member2...] 向集合中添加一个或多个成员 s ...
- 客户端相关知识学习(三)之Android原生与H5交互的实现
Android原生与H5交互的实现 H5调用原生的方式 方式可能有多种,根据开发经验,接触过两种方式. 方法一:Android向H5注入全局js对象,也就是H5调Android 1.首先对WebVie ...
- [转]Opcode是啥以及如何使用好Opcache
转载链接:Opcode是啥以及如何使用好Opcache 啥是Opcode? 我们在日常的PHP开发过程中,应该经常会听见Opcache这个词,那么啥是Opcode呢? Opcache 的前生是 Opt ...
- private修饰的方法可以通过反射访问,那么private的意义是什么?
反射代码: package test; public class Person { private String userName= "Tom"; private void pla ...