less than 1 minute read

2511. Maximum Enemy Forts That Can Be Captured (easy)

class Solution:
    def captureForts(self, forts: List[int]) -> int:
        n = len(forts)
        res = 0
        for i in range(n):
            if forts[i] != 1:
                continue
            j = k = i
            while j + 1 < n and forts[j + 1] == 0:
                j += 1
            while k - 1 >= 0 and forts[k - 1] == 0:
                k -= 1
            print(i, j, k)
            if j + 1 < n and forts[j + 1] == -1:
                res = max(res, j - i)
            if k - 1 >= 0 and forts[k - 1] == -1:
                res = max(res, i - k)


        return res