1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-11-12 15:25:48 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (English)/边界与内部和相等的稳定子数组(English) [stable-subarrays-with-equal-boundary-and-interior-sum].html
2025-11-11 22:54:49 +08:00

59 lines
2.2 KiB
HTML

<p>You are given an integer array <code>capacity</code>.</p>
<p>A <span data-keyword="subarray-nonempty">subarray</span> <code>capacity[l..r]</code> is considered <strong>stable</strong> if:</p>
<ul>
<li>Its length is <strong>at least</strong> 3.</li>
<li>The <strong>first</strong> and <strong>last</strong> elements are each equal to the <strong>sum</strong> of all elements <strong>strictly between</strong> them (i.e., <code>capacity[l] = capacity[r] = capacity[l + 1] + capacity[l + 2] + ... + capacity[r - 1]</code>).</li>
</ul>
<p>Return an integer denoting the number of <strong>stable subarrays</strong>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">capacity = [9,3,3,3,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>[9,3,3,3,9]</code> is stable because the first and last elements are both 9, and the sum of the elements strictly between them is <code>3 + 3 + 3 = 9</code>.</li>
<li><code>[3,3,3]</code> is stable because the first and last elements are both 3, and the sum of the elements strictly between them is 3.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">capacity = [1,2,3,4,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>No subarray of length at least 3 has equal first and last elements, so the answer is 0.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">capacity = [-4,4,0,0,-8,-4]</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p><code>[-4,4,0,0,-8,-4]</code> is stable because the first and last elements are both -4, and the sum of the elements strictly between them is <code>4 + 0 + 0 + (-8) = -4</code></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= capacity.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= capacity[i] &lt;= 10<sup>9</sup></code></li>
</ul>