Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
stickers-to-spell-word.py 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-13 01:56 +08:00 . add complexity
# Time: O(T * S^T)
# Space: O(T * S^T)
import collections
class Solution(object):
def minStickers(self, stickers, target):
"""
:type stickers: List[str]
:type target: str
:rtype: int
"""
def minStickersHelper(sticker_counts, target, dp):
if "".join(target) in dp:
return dp["".join(target)]
target_count = collections.Counter(target)
result = float("inf")
for sticker_count in sticker_counts:
if sticker_count[target[0]] == 0:
continue
new_target = []
for k in target_count.keys():
if target_count[k] > sticker_count[k]:
new_target += [k]*(target_count[k] - sticker_count[k])
if len(new_target) != len(target):
num = minStickersHelper(sticker_counts, new_target, dp)
if num != -1:
result = min(result, 1+num)
dp["".join(target)] = -1 if result == float("inf") else result
return dp["".join(target)]
sticker_counts = map(collections.Counter, stickers)
dp = { "":0 }
return minStickersHelper(sticker_counts, target, dp)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助