每日一题 2511. 最多可以摧毁的敌人城堡数目

难度:简单
每日一题 2511. 最多可以摧毁的敌人城堡数目_第1张图片
翻译:寻找距离最远的 1 和 -1 的组合,要求它们之间只有0

class Solution:
    def captureForts(self, forts: List[int]) -> int:
        res, t = 0, -1
        for i, fort in enumerate(forts):
            if fort == -1 or fort == 1:
                if t >= 0 and fort != forts[t]:
                    res = max(res, i - t - 1)
                t = i
        return res

你可能感兴趣的:(用Python刷力扣,python)