<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>

from enum import Enum

# class Solution:
    # def captureForts(self, forts: List[int]) -> int:
    #     lp = 0
    #     count= -1
    #     res = 0

    #     while lp < len(forts):
    #         if forts[lp] == 1:
    #             count = 0
    #             lp += 1
    #             while lp < len(forts) and forts[lp] not in [1, -1]:
    #                 lp += 1
    #                 count += 1
    #             if lp < len(forts):
    #                 if forts[lp] == -1:
    #                     res = max(res, count)
    #         elif forts[lp] == -1:
    #             count = 0
    #             lp += 1
    #             while lp < len(forts) and forts[lp] not in [1, -1]:
    #                 lp += 1
    #                 count += 1
    #             if lp < len(forts):
    #                 if forts[lp] == 1:
    #                     res = max(res, count)
    #         else:
    #             lp += 1

    #     return res   
class State(Enum):
    NEUTRAL    = 0
    WANT_FORT  = 1
    WANT_EMPTY = 2

class Solution:
    def captureForts(self, forts: List[int]) -> int:
        # We only care about two cases:
        # - Start at -1 and end at 1
        # - Start at 1 and end at -1
        res = 0
        p = 0
        state = State.NEUTRAL
        curr = 0

        while p < len(forts):
            space = forts[p]
            match state:
                case State.WANT_FORT:
                    if space == 0:
                        curr += 1
                    elif space == -1:
                        curr = 0
                    else:
                        res = max(res, curr)
                        curr = 0
                        state = State.WANT_EMPTY
                case State.WANT_EMPTY:
                    if space == 0:
                        curr += 1
                    elif space == 1:
                        curr = 0
                    else:
                        res = max(res, curr)
                        curr = 0
                        state = State.WANT_FORT
                case State.NEUTRAL:
                    if space == 1:
                        state = State.WANT_EMPTY
                    elif space == -1:
                        state = State.WANT_FORT
            p += 1

        return res




        
40