Class Solution
java.lang.Object
g2501_2600.s2511_maximum_enemy_forts_that_can_be_captured.Solution
2511 - Maximum Enemy Forts That Can Be Captured.<p>Easy</p>
<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 <= i, j <= 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) < k < 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><strong>Example 1:</strong></p>
<p><strong>Input:</strong> forts = [1,0,0,-1,0,0,0,0,1]</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong></p>
<ul>
<li>
<p>Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2.</p>
</li>
<li>
<p>Moving the army from position 8 to position 3 captures 4 enemy forts.</p>
</li>
</ul>
<p>Since 4 is the maximum number of enemy forts that can be captured, we return 4.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> forts = [0,0,1,-1]</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong> Since no enemy fort can be captured, 0 is returned.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= forts.length <= 1000</code></li>
<li><code>-1 <= forts[i] <= 1</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
captureForts
public int captureForts(int[] forts)
-