Count Stable Subarrays Problem
Description
LeetCode Problem 3748.
You are given an integer array nums.
A subarray of nums is called stable if it contains no inversions, i.e., there is no pair of indices i < j such that nums[i] > nums[j].
You are also given a 2D integer array queries of length q, where each queries[i] = [l_i, r_i] represents a query. For each query [l_i, r_i], compute the number of stable subarrays that lie entirely within the segment nums[l_i..r_i].
Return an integer array ans of length q, where ans[i] is the answer to the i^th query.
Note:
- A single element subarray is considered stable.
Example 1:
Input: nums = [3,1,2], queries = [[0,1],[1,2],[0,2]]
Output: [2,3,4]
Explanation:
- For queries[0] = [0, 1], the subarray is [nums[0], nums[1]] = [3, 1].
- The stable subarrays are [3] and [1]. The total number of stable subarrays is 2.
- For queries[1] = [1, 2], the subarray is [nums[1], nums[2]] = [1, 2].
- The stable subarrays are [1], [2], and [1, 2]. The total number of stable subarrays is 3.
- For queries[2] = [0, 2], the subarray is [nums[0], nums[1], nums[2]] = [3, 1, 2].
- The stable subarrays are [3], [1], [2], and [1, 2]. The total number of stable subarrays is 4.
Thus, ans = [2, 3, 4].
Example 2:
Input: nums = [2,2], queries = [[0,1],[0,0]]
Output: [3,1]
Explanation:
- For queries[0] = [0, 1], the subarray is [nums[0], nums[1]] = [2, 2].
- The stable subarrays are [2], [2], and [2, 2]. The total number of stable subarrays is 3.
- For queries[1] = [0, 0], the subarray is [nums[0]] = [2].
- The stable subarray is [2]. The total number of stable subarrays is 1.
Thus, ans = [3, 1].
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^51 <= queries.length <= 10^5queries[i] = [l_i, r_i]0 <= l_i <= r_i <= nums.length - 1
Solution
Approach: Split into non-decreasing runs
A subarray has no inversion exactly when it is non-decreasing. Cut the array into maximal non-decreasing runs. A run of length k contains k * (k + 1) / 2 stable subarrays, and a stable subarray never crosses a run boundary. Store the start of every run in seg and the prefix totals of run counts in s.
For a query [l, r], binary search the runs touched by it:
- If
landrfall in the same run, the answer isk * (k + 1) / 2withk = r - l + 1. - Otherwise the answer is the partial first run (from
lto its end), plus all the complete runs in between (froms), plus the partial last run (from its start tor), each partial one counted with the same triangular formula.
Complexity: time O((n + q) log n), space O(n).
from bisect import bisect_right
class Solution:
def countStableSubarrays(
self, nums: List[int], queries: List[List[int]]
) -> List[int]:
s = [0]
l, n = 0, len(nums)
seg = []
for r, x in enumerate(nums):
if r == n - 1 or x > nums[r + 1]:
seg.append(l)
k = r - l + 1
s.append(s[-1] + (1 + k) * k // 2)
l = r + 1
ans = []
for l, r in queries:
i = bisect_right(seg, l)
j = bisect_right(seg, r) - 1
if i > j:
k = r - l + 1
ans.append((1 + k) * k // 2)
else:
a = seg[i] - l
b = r - seg[j] + 1
ans.append((1 + a) * a // 2 + s[j] - s[i] + (1 + b) * b // 2)
return ans