mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
47 lines
2.1 KiB
HTML
47 lines
2.1 KiB
HTML
<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> </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> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= forts.length <= 1000</code></li>
|
|
<li><code>-1 <= forts[i] <= 1</code></li>
|
|
</ul>
|