[leetcode]Maximum Product of Word Lengths
Posted 阿牧遥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode]Maximum Product of Word Lengths相关的知识,希望对你有一定的参考价值。
用了python的set。为了效率,先做了预处理,并排序了。要注意,排序完才好预处理,否则i,j会对不上。
class Solution: def maxProduct(self, words: List[str]) -> int: maxProd = 0 words = sorted(words, key=lambda x:-len(x)) charSets = [] for i in range(len(words)): charSets.append(set(list(words[i]))) for i in range(len(words)): for j in range(i + 1, len(words)): if len(charSets[i] & charSets[j]) == 0: prod = len(words[i]) * len(words[j]) if prod > maxProd: maxProd = prod break return maxProd
以上是关于[leetcode]Maximum Product of Word Lengths的主要内容,如果未能解决你的问题,请参考以下文章
leetcode 152. Maximum Product Subarray
C#解leetcode 152. Maximum Product Subarray
Maximum Product Subarray Leetcode
[动态规划] leetcode 152 Maximum Product Subarray