mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
49 lines
2.1 KiB
HTML
49 lines
2.1 KiB
HTML
<p>The <strong>imbalance number</strong> of a <strong>0-indexed</strong> integer array <code>arr</code> of length <code>n</code> is defined as the number of indices in <code>sarr = sorted(arr)</code> such that:</p>
|
|
|
|
<ul>
|
|
<li><code>0 <= i < n - 1</code>, and</li>
|
|
<li><code>sarr[i+1] - sarr[i] > 1</code></li>
|
|
</ul>
|
|
|
|
<p>Here, <code>sorted(arr)</code> is the function that returns the sorted version of <code>arr</code>.</p>
|
|
|
|
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, return <em>the <strong>sum of imbalance numbers</strong> of all its <strong>subarrays</strong></em>.</p>
|
|
|
|
<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [2,3,1,4]
|
|
<strong>Output:</strong> 3
|
|
<strong>Explanation:</strong> There are 3 subarrays with non-zero<strong> </strong>imbalance numbers:
|
|
- Subarray [3, 1] with an imbalance number of 1.
|
|
- Subarray [3, 1, 4] with an imbalance number of 1.
|
|
- Subarray [1, 4] with an imbalance number of 1.
|
|
The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 3.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> nums = [1,3,3,3,5]
|
|
<strong>Output:</strong> 8
|
|
<strong>Explanation:</strong> There are 7 subarrays with non-zero imbalance numbers:
|
|
- Subarray [1, 3] with an imbalance number of 1.
|
|
- Subarray [1, 3, 3] with an imbalance number of 1.
|
|
- Subarray [1, 3, 3, 3] with an imbalance number of 1.
|
|
- Subarray [1, 3, 3, 3, 5] with an imbalance number of 2.
|
|
- Subarray [3, 3, 3, 5] with an imbalance number of 1.
|
|
- Subarray [3, 3, 5] with an imbalance number of 1.
|
|
- Subarray [3, 5] with an imbalance number of 1.
|
|
The imbalance number of all other subarrays is 0. Hence, the sum of imbalance numbers of all the subarrays of nums is 8. </pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= nums.length <= 1000</code></li>
|
|
<li><code>1 <= nums[i] <= nums.length</code></li>
|
|
</ul>
|