2602. Maximum Enemy Forts That Can Be Captured
My accepted Python solution to LeetCode problem 2602, Maximum Enemy Forts That Can Be Captured, running in 0ms.
- Difficulty: Easy
- Python
- Runtime 0ms
- Memory 17.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.3MB, accepted 2025-12-31.
class Solution:
def captureForts(self, forts: List[int]) -> int:
max_captured = 0
i = 0
while i < len(forts):
if forts[i] == 1:
# Start from your fort, look for empty (move to -1)
j = i + 1
while j < len(forts) and forts[j] == 0:
j += 1
if j < len(forts) and forts[j] == -1:
max_captured = max(max_captured, j - i - 1)
elif forts[i] == -1:
# Start from empty, look for enemy (move to 1)
j = i + 1
while j < len(forts) and forts[j] == 0:
j += 1
if j < len(forts) and forts[j] == 1:
max_captured = max(max_captured, j - i - 1)
i += 1
return max_captured