1996. 游戏中弱角色的数量
由于是比较两个值是否都大于,所以可以按照攻击值降序排序,
对于排序后遍历的每个元素的攻击值都是存在存在比他大的,所以只需维护一个防御值的当前最大就行,若当前元素的防御值小于当前最大,那么弱角色+1
class Solution:
def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
res = 0
properties = sorted(properties,key= lambda x:(-x[0],x[1]))
maxt = 0
for prop in properties:
if maxt > prop[1]:
res +=1
else :
maxt = prop[1]
return res
[916. 单词子集](https://leetcode-cn.com/problems/find-first-palindromic-string-in-the-array/)
首先 记录 word2 列表 所有单词的 字母并集,
对于word1 中的单词 ,判断是否大于该并集
class Solution:
def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
wdic = defaultdict(int)
res = []
for word in words2:
coun = Counter(word)
for key in coun:
if coun[key] > wdic.get(key,0):
wdic[key] =coun[key]
for word in words1:
coun = Counter(word)
flag =1
for key in wdic:
if coun[key] < wdic[key]:
flag =0
break
if flag:
res.append(word)
#print(wdic)
#print(a['e'])
return res