leetcode-162周赛-1255-得分最高的单词集合
题目描述:


方法:穷举暴力
class Solution:
def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int:
from collections import Counter
n = len(words)
v = lambda c: ord(c) - ord('a')
words = [Counter(v(c) for c in w) for w in words]
scores = [sum(score[k]*v for k, v in w.items()) for w in words]
avail = [0]*26
for c in letters: avail[v(c)] += 1
best = [0]
curr = [0]
def dfs(i):
if i == n:
best[0] = max(best[0], curr[0])
return
dfs(i+1)
if all(avail[k] >= v for k, v in words[i].items()):
for k, v in words[i].items(): avail[k] -= v
curr[0] += scores[i]
dfs(i+1)
curr[0] -= scores[i]
for k, v in words[i].items(): avail[k] += v
dfs(0)
return best[0]
leetcode-162周赛-1255-得分最高的单词集合的更多相关文章
- LeetCode 1255 得分最高的单词集合 Maximum Score Words Formed by Letters
地址 https://leetcode-cn.com/problems/maximum-score-words-formed-by-letters/ 题目描述你将会得到一份单词表 words,一个字母 ...
- [LeetCode] 244. Shortest Word Distance II 最短单词距离 II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
- [LeetCode] 245. Shortest Word Distance III 最短单词距离 III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Length of Last Word 求末尾单词的长度
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [LeetCode] Stickers to Spell Word 贴片拼单词
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- [LeetCode] Shortest Completing Word 最短完整的单词
Find the minimum length word from a given dictionary words, which has all the letters from the strin ...
- [LeetCode] Most Common Word 最常见的单词
Given a paragraph and a list of banned words, return the most frequent word that is not in the list ...
- [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)
Design a class which receives a list of words in the constructor, and implements a method that takes ...
- LeetCode(58): 最后一个单词的长度
Easy! 题目描述: 给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. ...
随机推荐
- diff算法(核心)
ps:大致转载知乎文章 vue和react的虚拟dom都采用类似的diff算法,核心大概可以归为两点 1,两个相同的组件产生类似的DOM结构,不同的组件产生不同的DOM结构: 2,同一层级的一组节点, ...
- java--split,index,StringTokenizer比较
import java.util.StringTokenizer; public class SplitDemo { //jdk8 public static void main(String[] a ...
- Halo(二)
@Conditional 满足条件给容器注册Bean(在配置类 @Configuration 的类和方法上配置) 需要实现Condition接口, 实现matches方法 public class L ...
- Mac上的Apache 开启,停止,重启
sudo apachectl -k start 启动 sudo apachectl -k stop 停止 sudo apachectl -k restart 重启
- 07-图5 Saving James Bond - Hard Version(30 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- oracle 查看所有表的数据量并排序
select t.table_name,t.num_rows from user_tables t ORDER BY NUM_ROWS DESC; 还可以直接查看dblink的:select t.ta ...
- vue常见面试题
什么是 mvvm? MVVM 是 Model-View-ViewModel 的缩写.mvvm 是一种设计思想.Model 层代表数据模型,也可以在 Model 中定义数据修改和操作的业务逻辑:View ...
- JavaScript实现注册时检查邮箱,名称,密码等是否符合规则
大概实现了,用户名是否存在,邮箱是否已注册,密码是否符合复杂度. //对用户名校验是否存在function checkname(){ //alert("checkname"); v ...
- php开发面试题---php缓存总结
php开发面试题---php缓存总结 一.总结 一句话总结: 缓存主要分本地缓存和分布式缓存两种 可以用分布式本地缓存:把那些常用的.不容易变的页面.数据都存下来 1.常用的缓存构架? 分布式本地缓存 ...
- python基础-包和模块
Python基础-包与模块 写在前面 如非特别说明,下文均基于Python3 摘要 为重用以及更好的维护代码,Python使用了模块与包:一个Python文件就是一个模块,包是组织模块的特殊目录(包含 ...