<p>You are given a <strong>0-indexed</strong> integer array <code>forts</code> of length <code>n</code> representing the positions of several forts. <code>forts[i]</code> can be <code>-1</code>, <code>0</code>, or <code>1</code> where:</p>

<ul>
	<li><code>-1</code> represents there is <strong>no fort</strong> at the <code>i<sup>th</sup></code> position.</li>
	<li><code>0</code> indicates there is an <strong>enemy</strong> fort at the <code>i<sup>th</sup></code> position.</li>
	<li><code>1</code> indicates the fort at the <code>i<sup>th</sup></code> the position is under your command.</li>
</ul>

<p>Now you have decided to move your army from one of your forts at position <code>i</code> to an empty position <code>j</code> such that:</p>

<ul>
	<li><code>0 &lt;= i, j &lt;= n - 1</code></li>
	<li>The army travels over enemy forts <strong>only</strong>. Formally, for all <code>k</code> where <code>min(i,j) &lt; k &lt; max(i,j)</code>, <code>forts[k] == 0.</code></li>
</ul>

<p>While moving the army, all the enemy forts that come in the way are <strong>captured</strong>.</p>

<p>Return<em> the <strong>maximum</strong> number of enemy forts that can be captured</em>. In case it is <strong>impossible</strong> to move your army, or you do not have any fort under your command, return <code>0</code><em>.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> forts = [1,0,0,-1,0,0,0,0,1]
<strong>Output:</strong> 4
<strong>Explanation:</strong>
- Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2.
- Moving the army from position 8 to position 3 captures 4 enemy forts.
Since 4 is the maximum number of enemy forts that can be captured, we return 4.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> forts = [0,0,1,-1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Since no enemy fort can be captured, 0 is returned.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= forts.length &lt;= 1000</code></li>
	<li><code>-1 &lt;= forts[i] &lt;= 1</code></li>
</ul>

class Solution:
    def captureForts(self, forts: List[int]) -> int:
        # # brute force
        # max_cnt, l = 0, len(forts)
        # # loop through all forts under command
        # for i in range(l):
        #     if forts[i] == 1:
        #         # move right
        #         j, cnt1 = 1, 0
        #         while i + j < l and forts[i + j] == 0:
        #             cnt1 += 1
        #             j += 1
        #         if i + j < l and forts[i + j] == -1:
        #             max_cnt = max(max_cnt, cnt1)
        #         # move left
        #         j, cnt2  = 1, 0
        #         while i - j >= 0 and forts[i - j] == 0:
        #             cnt2 += 1
        #             j += 1
        #         if i - j >= 0 and forts[i - j] == -1:
        #             max_cnt = max(max_cnt, cnt2)
        # return max_cnt
        
        # find the largest distance between 1 and -1 next to each other
        ans, pre = 0, -1
        for i, fort in enumerate(forts):
            # possible start and end location
            if fort == -1 or fort == 1:
                # i and pre are a pair of {1, -1}
                if pre >= 0 and fort != forts[pre]:
                    # i - pre - 1 is the distance between them 
                    ans = max(ans, i - pre - 1)
                # update pre
                pre = i
        return ans



24