1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-12-15 23:22:36 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2025-11-11 22:54:49 +08:00
parent 73ae78c4b4
commit 1ea1df8784
65 changed files with 17243 additions and 10240 deletions

View File

@@ -0,0 +1,57 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>You <strong>must</strong> replace <strong>exactly one</strong> element in the array with <strong>any</strong> integer value in the range <code>[-10<sup>5</sup>, 10<sup>5</sup>]</code> (inclusive).</p>
<p>After performing this single replacement, determine the <strong>maximum possible product</strong> of <strong>any three</strong> elements at <strong>distinct indices</strong> from the modified array.</p>
<p>Return an integer denoting the <strong>maximum product</strong> achievable.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-5,7,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">3500000</span></p>
<p><strong>Explanation:</strong></p>
<p>Replacing 0 with -10<sup>5</sup> gives the array <code>[-5, 7, -10<sup>5</sup>]</code>, which has a product <code>(-5) * 7 * (-10<sup>5</sup>) = 3500000</code>. The maximum product is 3500000.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [-4,-2,-1,-3]</span></p>
<p><strong>Output:</strong> <span class="example-io">1200000</span></p>
<p><strong>Explanation:</strong></p>
<p>Two ways to achieve the maximum product include:</p>
<ul>
<li><code>[-4, -2, -3]</code> &rarr; replace -2 with 10<sup>5</sup> &rarr; product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.</li>
<li><code>[-4, -1, -3]</code> &rarr; replace -1 with 10<sup>5</sup> &rarr; product = <code>(-4) * 10<sup>5</sup> * (-3) = 1200000</code>.</li>
</ul>
The maximum product is 1200000.</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [0,10,0]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no way to replace an element with another integer and not have a 0 in the array. Hence, the product of all three elements will always be 0, and the maximum product is 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>3 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,56 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>A tuple <code>(i, j, k)</code> of 3 <strong>distinct</strong> indices is <strong>good</strong> if <code>nums[i] == nums[j] == nums[k]</code>.</p>
<p>The <strong>distance</strong> of a <strong>good</strong> tuple is <code>abs(i - j) + abs(j - k) + abs(k - i)</code>, where <code>abs(x)</code> denotes the <strong>absolute value</strong> of <code>x</code>.</p>
<p>Return an integer denoting the <strong>minimum</strong> possible <strong>distance</strong> of a <strong>good</strong> tuple. If no <strong>good</strong> tuples exist, return <code>-1</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The minimum distance is achieved by the good tuple <code>(0, 2, 3)</code>.</p>
<p><code>(0, 2, 3)</code> is a good tuple because <code>nums[0] == nums[2] == nums[3] == 1</code>. Its distance is <code>abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,3,2,1,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<p>The minimum distance is achieved by the good tuple <code>(2, 4, 6)</code>.</p>
<p><code>(2, 4, 6)</code> is a good tuple because <code>nums[2] == nums[4] == nums[6] == 2</code>. Its distance is <code>abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no good tuples. Therefore, the answer is -1.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= n</code></li>
</ul>

View File

@@ -0,0 +1,56 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>A tuple <code>(i, j, k)</code> of 3 <strong>distinct</strong> indices is <strong>good</strong> if <code>nums[i] == nums[j] == nums[k]</code>.</p>
<p>The <strong>distance</strong> of a <strong>good</strong> tuple is <code>abs(i - j) + abs(j - k) + abs(k - i)</code>, where <code>abs(x)</code> denotes the <strong>absolute value</strong> of <code>x</code>.</p>
<p>Return an integer denoting the <strong>minimum</strong> possible <strong>distance</strong> of a <strong>good</strong> tuple. If no <strong>good</strong> tuples exist, return <code>-1</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The minimum distance is achieved by the good tuple <code>(0, 2, 3)</code>.</p>
<p><code>(0, 2, 3)</code> is a good tuple because <code>nums[0] == nums[2] == nums[3] == 1</code>. Its distance is <code>abs(0 - 2) + abs(2 - 3) + abs(3 - 0) = 2 + 1 + 3 = 6</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,3,2,1,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>Explanation:</strong></p>
<p>The minimum distance is achieved by the good tuple <code>(2, 4, 6)</code>.</p>
<p><code>(2, 4, 6)</code> is a good tuple because <code>nums[2] == nums[4] == nums[6] == 2</code>. Its distance is <code>abs(2 - 4) + abs(4 - 6) + abs(6 - 2) = 2 + 2 + 4 = 8</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1]</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no good tuples. Therefore, the answer is -1.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= n</code></li>
</ul>

View File

@@ -0,0 +1,64 @@
<p>You are given a string <code>s</code> of length <code>n</code> consisting of lowercase English letters.</p>
<p>You must perform <strong>exactly</strong> one operation by choosing any integer <code>k</code> such that <code>1 &lt;= k &lt;= n</code> and either:</p>
<ul>
<li>reverse the <strong>first</strong> <code>k</code> characters of <code>s</code>, or</li>
<li>reverse the <strong>last</strong> <code>k</code> characters of <code>s</code>.</li>
</ul>
<p>Return the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span></strong> string that can be obtained after <strong>exactly</strong> one such operation.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;dcab&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;acdb&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Choose <code>k = 3</code>, reverse the first 3 characters.</li>
<li>Reverse <code>&quot;dca&quot;</code> to <code>&quot;acd&quot;</code>, resulting string <code>s = &quot;acdb&quot;</code>, which is the lexicographically smallest string achievable.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;abba&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;aabb&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Choose <code>k = 3</code>, reverse the last 3 characters.</li>
<li>Reverse <code>&quot;bba&quot;</code> to <code>&quot;abb&quot;</code>, so the resulting string is <code>&quot;aabb&quot;</code>, which is the lexicographically smallest string achievable.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;zxy&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;xzy&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Choose <code>k = 2</code>, reverse the first 2 characters.</li>
<li>Reverse <code>&quot;zx&quot;</code> to <code>&quot;xz&quot;</code>, so the resulting string is <code>&quot;xzy&quot;</code>, which is the lexicographically smallest string achievable.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == s.length &lt;= 1000</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>

View File

@@ -0,0 +1,69 @@
<p>You are given two strings <code>s</code> and <code>target</code>, each of length <code>n</code>, consisting of lowercase English letters.</p>
<p>Return the <strong><span data-keyword="lexicographically-smaller-string">lexicographically smallest</span> string</strong> that is <strong>both</strong> a <strong><span data-keyword="palindrome-string">palindromic</span> <span data-keyword="permutation">permutation</span></strong> of <code>s</code> and <strong>strictly</strong> greater than <code>target</code>. If no such permutation exists, return an empty string.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;baba&quot;, target = &quot;abba&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;baab&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The palindromic permutations of <code>s</code> (in lexicographical order) are <code>&quot;abba&quot;</code> and <code>&quot;baab&quot;</code>.</li>
<li>The lexicographically smallest permutation that is strictly greater than <code>target</code> is <code>&quot;baab&quot;</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;baba&quot;, target = &quot;bbaa&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The palindromic permutations of <code>s</code> (in lexicographical order) are <code>&quot;abba&quot;</code> and <code>&quot;baab&quot;</code>.</li>
<li>None of them is lexicographically strictly greater than <code>target</code>. Therefore, the answer is <code>&quot;&quot;</code>.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;abc&quot;, target = &quot;abb&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p><code>s</code> has no palindromic permutations. Therefore, the answer is <code>&quot;&quot;</code>.</p>
</div>
<p><strong class="example">Example 4:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;aac&quot;, target = &quot;abb&quot;</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;aca&quot;</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The only palindromic permutation of <code>s</code> is <code>&quot;aca&quot;</code>.</li>
<li><code>&quot;aca&quot;</code> is strictly greater than <code>target</code>. Therefore, the answer is <code>&quot;aca&quot;</code>.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == s.length == target.length &lt;= 300</code></li>
<li><code>s</code> and <code>target</code> consist of only lowercase English letters.</li>
</ul>

View File

@@ -0,0 +1,65 @@
<p>You are given two integer arrays of size 2: <code>d = [d<sub>1</sub>, d<sub>2</sub>]</code> and <code>r = [r<sub>1</sub>, r<sub>2</sub>]</code>.</p>
<p>Two delivery drones are tasked with completing a specific number of deliveries. Drone <code>i</code> must complete <code>d<sub>i</sub></code> deliveries.</p>
<p>Each delivery takes <strong>exactly</strong> one hour and <strong>only one</strong> drone can make a delivery at any given hour.</p>
<p>Additionally, both drones require recharging at specific intervals during which they cannot make deliveries. Drone <code>i</code> must recharge every <code>r<sub>i</sub></code> hours (i.e. at hours that are multiples of <code>r<sub>i</sub></code>).</p>
<p>Return an integer denoting the <strong>minimum</strong> total time (in hours) required to complete all deliveries.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">d = [3,1], r = [2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The first drone delivers at hours 1, 3, 5 (recharges at hours 2, 4).</li>
<li>The second drone delivers at hour 2 (recharges at hour 3).</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">d = [1,3], r = [2,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">7</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The first drone delivers at hour 3 (recharges at hours 2, 4, 6).</li>
<li>The second drone delivers at hours 1, 5, 7 (recharges at hours 2, 4, 6).</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">d = [2,1], r = [3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>The first drone delivers at hours 1, 2 (recharges at hour 3).</li>
<li>The second drone delivers at hour 3.</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>d = [d<sub>1</sub>, d<sub>2</sub>]</code></li>
<li><code>1 &lt;= d<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
<li><code>r = [r<sub>1</sub>, r<sub>2</sub>]</code></li>
<li><code>2 &lt;= r<sub>i</sub> &lt;= 3 * 10<sup>4</sup></code></li>
</ul>

View File

@@ -0,0 +1,62 @@
<p>You are given a <strong>cyclic</strong> array <code>nums</code> and an integer <code>k</code>.</p>
<p><strong>Partition</strong> <code>nums</code> into <strong>at most</strong> <code>k</code><strong> </strong><span data-keyword="subarray-nonempty">subarrays</span>. As <code>nums</code> is cyclic, these subarrays may wrap around from the end of the array back to the beginning.</p>
<p>The <strong>range</strong> of a subarray is the difference between its <strong>maximum</strong> and <strong>minimum</strong> values. The <strong>score</strong> of a partition is the sum of subarray <strong>ranges</strong>.</p>
<p>Return the <strong>maximum</strong> possible <strong>score</strong> among all cyclic partitions.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,3], k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Partition <code>nums</code> into <code>[2, 3]</code> and <code>[3, 1]</code> (wrapped around).</li>
<li>The range of <code>[2, 3]</code> is <code>max(2, 3) - min(2, 3) = 3 - 2 = 1</code>.</li>
<li>The range of <code>[3, 1]</code> is <code>max(3, 1) - min(3, 1) = 3 - 1 = 2</code>.</li>
<li>The score is <code>1 + 2 = 3</code>.</li>
</ul>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,3], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<ul>
<li>Partition <code>nums</code> into <code>[1, 2, 3, 3]</code>.</li>
<li>The range of <code>[1, 2, 3, 3]</code> is <code>max(1, 2, 3, 3) - min(1, 2, 3, 3) = 3 - 1 = 2</code>.</li>
<li>The score is 2.</li>
</ul>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,3], k = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>Identical to Example 1, we partition <code>nums</code> into <code>[2, 3]</code> and <code>[3, 1]</code>. Note that <code>nums</code> may be partitioned into fewer than <code>k</code> subarrays.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

View File

@@ -0,0 +1,52 @@
<p>You are given an integer array <code>nums</code> consisting of <strong>unique</strong> integers.</p>
<p>Originally, <code>nums</code> contained <strong>every integer</strong> within a certain range. However, some integers might have gone <strong>missing</strong> from the array.</p>
<p>The <strong>smallest</strong> and <strong>largest</strong> integers of the original range are still present in <code>nums</code>.</p>
<p>Return a <strong>sorted</strong> list of all the missing integers in this range. If no integers are missing, return an <strong>empty</strong> list.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,2,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">[3]</span></p>
<p><strong>Explanation:</strong></p>
<p>The smallest integer is 1 and the largest is 5, so the full range should be <code>[1,2,3,4,5]</code>. Among these, only 3 is missing.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [7,8,6,9]</span></p>
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
<p><strong>Explanation:</strong></p>
<p>The smallest integer is 6 and the largest is 9, so the full range is <code>[6,7,8,9]</code>. All integers are already present, so no integer is missing.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,1]</span></p>
<p><strong>Output:</strong> <span class="example-io">[2,3,4]</span></p>
<p><strong>Explanation:</strong></p>
<p>The smallest integer is 1 and the largest is 5, so the full range should be <code>[1,2,3,4,5]</code>. The missing integers are 2, 3, and 4.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,72 @@
<p>You are given two <strong>positive</strong> integers <code>num</code> and <code>sum</code>.</p>
<p>A positive integer <code>n</code> is <strong>good</strong> if it satisfies both of the following:</p>
<ul>
<li>The number of digits in <code>n</code> is <strong>exactly</strong> <code>num</code>.</li>
<li>The sum of digits in <code>n</code> is <strong>exactly</strong> <code>sum</code>.</li>
</ul>
<p>The <strong>score</strong> of a <strong>good</strong> integer <code>n</code> is the sum of the squares of digits in <code>n</code>.</p>
<p>Return a <strong>string</strong> denoting the <strong>good</strong> integer <code>n</code> that achieves the <strong>maximum</strong> <strong>score</strong>. If there are multiple possible integers, return the <strong>maximum </strong>one. If no such integer exists, return an empty string.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num = 2, sum = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;30&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>There are 3 good integers: 12, 21, and 30.</p>
<ul>
<li>The score of 12 is <code>1<sup>2</sup> + 2<sup>2</sup> = 5</code>.</li>
<li>The score of 21 is <code>2<sup>2</sup> + 1<sup>2</sup> = 5</code>.</li>
<li>The score of 30 is <code>3<sup>2</sup> + 0<sup>2</sup> = 9</code>.</li>
</ul>
<p>The maximum score is 9, which is achieved by the good integer 30. Therefore, the answer is <code>&quot;30&quot;</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num = 2, sum = 17</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;98&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>There are 2 good integers: 89 and 98.</p>
<ul>
<li>The score of 89 is <code>8<sup>2</sup> + 9<sup>2</sup> = 145</code>.</li>
<li>The score of 98 is <code>9<sup>2</sup> + 8<sup>2</sup> = 145</code>.</li>
</ul>
<p>The maximum score is 145. The maximum good integer that achieves this score is 98. Therefore, the answer is <code>&quot;98&quot;</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">num = 1, sum = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">&quot;&quot;</span></p>
<p><strong>Explanation:</strong></p>
<p>There are no integers that have exactly 1 digit and whose digits sum to 10. Therefore, the answer is <code>&quot;&quot;</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= num &lt;= 2 * 10<sup>5</sup></code></li>
<li><code>1 &lt;= sum &lt;= 2 * 10<sup>6</sup></code></li>
</ul>

View File

@@ -0,0 +1,42 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>You are allowed to replace <strong>at most</strong> one element in the array with any other integer value of your choice.</p>
<p>Return the length of the <strong>longest non-decreasing <span data-keyword="subarray">subarray</span></strong> that can be obtained after performing at most one replacement.</p>
<p>An array is said to be <strong>non-decreasing</strong> if each element is greater than or equal to its previous one (if it exists).</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,1,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<p>Replacing <code>nums[3] = 1</code> with 3 gives the array [1, 2, 3, 3, 2].</p>
<p>The longest non-decreasing subarray is [1, 2, 3, 3], which has a length of 4.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,2,2,2]</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>All elements in <code>nums</code> are equal, so it is already non-decreasing and the entire <code>nums</code> forms a subarray of length 5.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,50 @@
<p>You are given an integer array <code>nums</code>. You may <strong>rearrange the elements</strong> in any order.</p>
<p>The <strong>alternating score</strong> of an array <code>arr</code> is defined as:</p>
<ul>
<li><code>score = arr[0]<sup>2</sup> - arr[1]<sup>2</sup> + arr[2]<sup>2</sup> - arr[3]<sup>2</sup> + ...</code></li>
</ul>
<p>Return an integer denoting the <strong>maximum possible alternating score</strong> of <code>nums</code> after rearranging its elements.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">12</span></p>
<p><strong>Explanation:</strong></p>
<p>A possible rearrangement for <code>nums</code> is <code>[2,1,3]</code>, which gives the maximum alternating score among all possible rearrangements.</p>
<p>The alternating score is calculated as:</p>
<p><code>score = 2<sup>2</sup> - 1<sup>2</sup> + 3<sup>2</sup> = 4 - 1 + 9 = 12</code></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,-1,2,-2,3,-3]</span></p>
<p><strong>Output:</strong> <span class="example-io">16</span></p>
<p><strong>Explanation:</strong></p>
<p>A possible rearrangement for <code>nums</code> is <code>[-3,-1,-2,1,3,2]</code>, which gives the maximum alternating score among all possible rearrangements.</p>
<p>The alternating score is calculated as:</p>
<p><code>score = (-3)<sup>2</sup> - (-1)<sup>2</sup> + (-2)<sup>2</sup> - (1)<sup>2</sup> + (3)<sup>2</sup> - (2)<sup>2</sup> = 9 - 1 + 4 - 1 + 9 - 4 = 16</code></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>-4 * 10<sup>4</sup> &lt;= nums[i] &lt;= 4 * 10<sup>4</sup></code></li>
</ul>

View File

@@ -0,0 +1,53 @@
<p>You are given an integer array <code>nums</code>.</p>
<p>In one move, you may <strong>increase</strong> the value of any single element <code>nums[i]</code> by 1.</p>
<p>Return the <strong>minimum total</strong> number of <strong>moves</strong> required so that all elements in <code>nums</code> become <strong>equal</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">nums = [2,1,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>To make all elements equal:</p>
<ul>
<li>Increase <code>nums[0] = 2</code> by 1 to make it 3.</li>
<li>Increase <code>nums[1] = 1</code> by 1 to make it 2.</li>
<li>Increase <code>nums[1] = 2</code> by 1 to make it 3.</li>
</ul>
<p>Now, all elements of <code>nums</code> are equal to 3. The minimum total moves is <code>3</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [4,4,5]</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>To make all elements equal:</p>
<ul>
<li>Increase <code>nums[0] = 4</code> by 1 to make it 5.</li>
<li>Increase <code>nums[1] = 4</code> by 1 to make it 5.</li>
</ul>
<p>Now, all elements of <code>nums</code> are equal to 5. The minimum total moves is <code>2</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>

View File

@@ -0,0 +1,35 @@
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p>
<p>Return the integer obtained by removing all zeros from the decimal representation of <code>n</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1020030</span></p>
<p><strong>Output:</strong> <span class="example-io">123</span></p>
<p><strong>Explanation:</strong></p>
<p>After removing all zeros from 1<strong><u>0</u></strong>2<strong><u>00</u></strong>3<strong><u>0</u></strong>, we get 123.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">n = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><strong>Explanation:</strong></p>
<p>1 has no zero in its decimal representation. Therefore, the answer is 1.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 10<sup>15</sup></code></li>
</ul>

View File

@@ -0,0 +1,61 @@
<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>
<p>Return the number of <strong><span data-keyword="subarray-nonempty">subarrays</span></strong> of <code>nums</code> in which <code>target</code> is the <strong>majority element</strong>.</p>
<p>The <strong>majority element</strong> of a subarray is the element that appears <strong>strictly</strong> <strong>more than half</strong> of the times in that subarray.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,2,3], target = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>Valid subarrays with <code>target = 2</code> as the majority element:</p>
<ul>
<li><code>nums[1..1] = [2]</code></li>
<li><code>nums[2..2] = [2]</code></li>
<li><code>nums[1..2] = [2,2]</code></li>
<li><code>nums[0..2] = [1,2,2]</code></li>
<li><code>nums[1..3] = [2,2,3]</code></li>
</ul>
<p>So there are 5 such subarrays.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1], target = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation: </strong></p>
<p><strong></strong>All 10 subarrays have 1 as the majority element.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], target = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p><code>target = 4</code> does not appear in <code>nums</code> at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= target &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,61 @@
<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>
<p>Return the number of <strong><span data-keyword="subarray-nonempty">subarrays</span></strong> of <code>nums</code> in which <code>target</code> is the <strong>majority element</strong>.</p>
<p>The <strong>majority element</strong> of a subarray is the element that appears <strong>strictly more than half</strong> of the times in that subarray.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,2,3], target = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">5</span></p>
<p><strong>Explanation:</strong></p>
<p>Valid subarrays with <code>target = 2</code> as the majority element:</p>
<ul>
<li><code>nums[1..1] = [2]</code></li>
<li><code>nums[2..2] = [2]</code></li>
<li><code>nums[1..2] = [2,2]</code></li>
<li><code>nums[0..2] = [1,2,2]</code></li>
<li><code>nums[1..3] = [2,2,3]</code></li>
</ul>
<p>So there are 5 such subarrays.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1,1], target = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">10</span></p>
<p><strong>Explanation: </strong></p>
<p><strong></strong>All 10 subarrays have 1 as the majority element.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], target = 4</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p><code>target = 4</code> does not appear in <code>nums</code> at all. Therefore, there cannot be any subarray where 4 is the majority element. Hence the answer is 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>1 &lt;= target &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,44 @@
<p>You are given an integer array <code>nums</code> <strong>sorted</strong> in <strong>non-descending</strong> order and a positive integer <code>k</code>.</p>
<p>A <strong><span data-keyword="subarray-nonempty">subarray</span></strong> of <code>nums</code> is <strong>good</strong> if the sum of its elements is <strong>divisible</strong> by <code>k</code>.</p>
<p>Return an integer denoting the number of <strong>distinct</strong> <strong>good</strong> subarrays of <code>nums</code>.</p>
<p>Subarrays are <strong>distinct</strong> if their sequences of values are. For example, there are 3 <strong>distinct</strong> subarrays in <code>[1, 1, 1]</code>, namely <code>[1]</code>, <code>[1, 1]</code>, and <code>[1, 1, 1]</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3], k = 3</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<p>The good subarrays are <code>[1, 2]</code>, <code>[3]</code>, and <code>[1, 2, 3]</code>. For example, <code>[1, 2, 3]</code> is good because the sum of its elements is <code>1 + 2 + 3 = 6</code>, and <code>6 % k = 6 % 3 = 0</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,2,2,2,2,2], k = 6</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The good subarrays are <code>[2, 2, 2]</code> and <code>[2, 2, 2, 2, 2, 2]</code>. For example, <code>[2, 2, 2]</code> is good because the sum of its elements is <code>2 + 2 + 2 = 6</code>, and <code>6 % k = 6 % 6 = 0</code>.</p>
<p>Note that <code>[2, 2, 2]</code> is counted only once.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
<li><code>nums</code> is sorted in non-descending order.</li>
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
</ul>

View File

@@ -0,0 +1,69 @@
<p>You are given a <code>m x n</code> matrix <code>mat</code> of positive integers.</p>
<p>Return an integer denoting the number of ways to choose <strong>exactly one</strong> integer from each row of <code>mat</code> such that the <strong>greatest common divisor</strong> of all chosen integers is 1.</p>
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">mat = [[1,2],[3,4]]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<tbody>
<tr>
<th align="center" style="border: 1px solid black;">Chosen integer in the first row</th>
<th align="center" style="border: 1px solid black;">Chosen integer in the second row</th>
<th align="center" style="border: 1px solid black;">Greatest common divisor of chosen integers</th>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">1</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">2</td>
</tr>
</tbody>
</table>
<p>3 of these combinations have a greatest common divisor of 1. Therefore, the answer is 3.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">mat = [[2,2],[2,2]]</span></p>
<p><strong>Output:</strong> <span class="example-io">0</span></p>
<p><strong>Explanation:</strong></p>
<p>Every combination has a greatest common divisor of 2. Therefore, the answer is 0.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m == mat.length &lt;= 150</code></li>
<li><code>1 &lt;= n == mat[i].length &lt;= 150</code></li>
<li><code>1 &lt;= mat[i][j] &lt;= 150</code></li>
</ul>

View File

@@ -0,0 +1,93 @@
<p>You are given an <code>m x n</code> grid where each cell contains one of the values 0, 1, or 2. You are also given an integer <code>k</code>.</p>
<p>You start from the top-left corner <code>(0, 0)</code> and want to reach the bottom-right corner <code>(m - 1, n - 1)</code> by moving only <strong>right</strong> or <strong>down</strong>.</p>
<p>Each cell contributes a specific score and incurs an associated cost, according to their cell values:</p>
<ul>
<li>0: adds 0 to your score and costs 0.</li>
<li>1: adds 1 to your score and costs 1.</li>
<li>2: adds 2 to your score and costs 1. </li>
</ul>
<p>Return the <strong>maximum</strong> score achievable without exceeding a total cost of <code>k</code>, or -1 if no valid path exists.</p>
<p><strong>Note:</strong> If you reach the last cell but the total cost exceeds <code>k</code>, the path is invalid.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0, 1],[2, 0]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">2</span></p>
<p><strong>Explanation:</strong></p>
<p>The optimal path is:</p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th style="border: 1px solid black;">Cell</th>
<th style="border: 1px solid black;">grid[i][j]</th>
<th style="border: 1px solid black;">Score</th>
<th style="border: 1px solid black;">Total<br />
Score</th>
<th style="border: 1px solid black;">Cost</th>
<th style="border: 1px solid black;">Total<br />
Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td style="border: 1px solid black;">(0, 0)</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">0</td>
</tr>
<tr>
<td style="border: 1px solid black;">(1, 0)</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">1</td>
<td style="border: 1px solid black;">1</td>
</tr>
<tr>
<td style="border: 1px solid black;">(1, 1)</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">2</td>
<td style="border: 1px solid black;">0</td>
<td style="border: 1px solid black;">1</td>
</tr>
</tbody>
</table>
<p>Thus, the maximum possible score is 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[0, 1],[1, 2]], k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no path that reaches cell <code>(1, 1)</code> without exceeding cost k. Thus, the answer is -1.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= m, n &lt;= 200</code></li>
<li><code>0 &lt;= k &lt;= 10<sup>3</sup></code></li>
<li><code><sup></sup>grid[0][0] == 0</code></li>
<li><code>0 &lt;= grid[i][j] &lt;= 2</code></li>
</ul>

View File

@@ -0,0 +1,178 @@
<p data-end="180" data-start="93">You are given two integer arrays <code>nums1</code> of length <code>n</code> and <code>nums2</code> of length <code>n + 1</code>.</p>
<p>You want to transform <code>nums1</code> into <code>nums2</code> using the <strong>minimum</strong> number of operations.</p>
<p>You may perform the following operations <strong>any</strong> number of times, each time choosing an index <code>i</code>:</p>
<ul>
<li><strong>Increase</strong> <code>nums1[i]</code> by 1.</li>
<li><strong>Decrease</strong> <code>nums1[i]</code> by 1.</li>
<li><strong>Append</strong> <code>nums1[i]</code> to the <strong>end</strong> of the array.</li>
</ul>
<p>Return the <strong>minimum</strong> number of operations required to transform <code>nums1</code> into <code>nums2</code>.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums1 = [2,8], nums2 = [1,7,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;"><code>i</code></th>
<th align="center" style="border: 1px solid black;">Operation</th>
<th align="center" style="border: 1px solid black;"><code>nums1[i]</code></th>
<th align="center" style="border: 1px solid black;">Updated <code>nums1</code></th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">Append</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[2, 8, 2]</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">Decrement</td>
<td align="center" style="border: 1px solid black;">Decreases to 1</td>
<td align="center" style="border: 1px solid black;">[1, 8, 2]</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">Decrement</td>
<td align="center" style="border: 1px solid black;">Decreases to 7</td>
<td align="center" style="border: 1px solid black;">[1, 7, 2]</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">Increment</td>
<td align="center" style="border: 1px solid black;">Increases to 3</td>
<td align="center" style="border: 1px solid black;">[1, 7, 3]</td>
</tr>
</tbody>
</table>
<p>Thus, after 4 operations <code>nums1</code> is transformed into <code>nums2</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums1 = [1,3,6], nums2 = [2,4,5,3]</span></p>
<p><strong>Output:</strong> <span class="example-io">4</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;"><code>i</code></th>
<th align="center" style="border: 1px solid black;">Operation</th>
<th align="center" style="border: 1px solid black;"><code>nums1[i]</code></th>
<th align="center" style="border: 1px solid black;">Updated <code>nums1</code></th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">Append</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[1, 3, 6, 3]</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">Increment</td>
<td align="center" style="border: 1px solid black;">Increases to 2</td>
<td align="center" style="border: 1px solid black;">[2, 3, 6, 3]</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">Increment</td>
<td align="center" style="border: 1px solid black;">Increases to 4</td>
<td align="center" style="border: 1px solid black;">[2, 4, 6, 3]</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">4</td>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">Decrement</td>
<td align="center" style="border: 1px solid black;">Decreases to 5</td>
<td align="center" style="border: 1px solid black;">[2, 4, 5, 3]</td>
</tr>
</tbody>
</table>
<p>Thus, after 4 operations <code>nums1</code> is transformed into <code>nums2</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums1 = [2], nums2 = [3,4]</span></p>
<p><strong>Output:</strong> <span class="example-io">3</span></p>
<p><strong>Explanation:</strong></p>
<table style="border: 1px solid black;">
<thead>
<tr>
<th align="center" style="border: 1px solid black;">Step</th>
<th align="center" style="border: 1px solid black;"><code>i</code></th>
<th align="center" style="border: 1px solid black;">Operation</th>
<th align="center" style="border: 1px solid black;"><code>nums1[i]</code></th>
<th align="center" style="border: 1px solid black;">Updated <code>nums1</code></th>
</tr>
</thead>
<tbody>
<tr>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">Increment</td>
<td align="center" style="border: 1px solid black;">Increases to 3</td>
<td align="center" style="border: 1px solid black;">[3]</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">2</td>
<td align="center" style="border: 1px solid black;">0</td>
<td align="center" style="border: 1px solid black;">Append</td>
<td align="center" style="border: 1px solid black;">-</td>
<td align="center" style="border: 1px solid black;">[3, 3]</td>
</tr>
<tr>
<td align="center" style="border: 1px solid black;">3</td>
<td align="center" style="border: 1px solid black;">1</td>
<td align="center" style="border: 1px solid black;">Increment</td>
<td align="center" style="border: 1px solid black;">Increases to 4</td>
<td align="center" style="border: 1px solid black;">[3, 4]</td>
</tr>
</tbody>
</table>
<p>Thus, after 3 operations <code>nums1</code> is transformed into <code>nums2</code>.</p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n == nums1.length &lt;= 10<sup>5</sup></code></li>
<li><code>nums2.length == n + 1</code></li>
<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>5</sup></code></li>
</ul>

View File

@@ -0,0 +1,58 @@
<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>