讨论/题目交流/🏆 第 168 场力扣周赛/
🏆 第 168 场力扣周赛

欢迎小伙伴们在这里交流分享你的参赛心得以及体验。【前往竞赛

image.png

3 分 - 统计位数为偶数的数字
4 分 - 划分数组为连续数字的集合
6 分 - 子串的最大出现次数
7 分 - 你能从盒子里获得的最大糖果数

展开讨论

这次第四题难度略水撒~
不要问,问就是面向对象强行怼!

class Box:
    def __init__(self, ID, status, candies, keys, containedBoxes):
        self.ID = ID
        self.status = status
        self.candies = candies
        self.keys = keys
        self.containedBoxes = containedBoxes
        self.accessed = False


class Solution:

    boxList = list()
    accessedBox = set()
    accessedKey = set()
    total = 0

    def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes) -> int:
        n = len(status)
        self.boxList = list()
        for i in range(n):
            curr = Box(i, status[i], candies[i], keys[i], containedBoxes[i])
            self.boxList.append(curr)

        self.accessedBox.clear()
        self.accessedKey.clear()
        self.total = 0

        for root in initialBoxes:
            rootBox = self.boxList[root]
            self.consTree(rootBox)

        for idx in self.accessedBox:
            idxBox = self.boxList[idx]
            if idxBox.ID in self.accessedKey or idxBox.status:
                self.total += idxBox.candies

        return self.total

    def consTree(self, box):
        self.accessedBox.add(box.ID)
        # self.accessedBox |= set(box.containedBoxes)
        self.accessedKey |= set(box.keys)
        if box.containedBoxes:
            for child in box.containedBoxes:
                childBox = self.boxList[child]
                self.consTree(childBox)
        return

1
展开全部 16 讨论