Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package g3701_3800.s3748_count_stable_subarrays;

// #Hard #Array #Binary_Search #Prefix_Sum #Senior_Staff #Weekly_Contest_476
// #2026_04_26_Time_9_ms_(100.00%)_Space_183.68_MB_(43.30%)

public class Solution {
public long[] countStableSubarrays(int[] nums, int[][] queries) {
int n = nums.length;
long[] preSum = new long[n + 1];
int[] idx = new int[n];
int[] end = new int[n];
int cnt = 0;
int prv = -1;
for (int i = 0; i < n; i++) {
if (nums[i] >= prv) {
cnt++;
} else {
cnt = 1;
}
prv = nums[i];
preSum[i + 1] = preSum[i] + cnt;
idx[i] = cnt - 1;
}
end[n - 1] = n - 1;
for (int i = n - 2; i >= 0; i--) {
if (idx[i] + 1 == idx[i + 1]) {
end[i] = end[i + 1];
} else {
end[i] = i;
}
}
long[] ans = new long[queries.length];
for (int l = 0; l < queries.length; l++) {
int i = queries[l][0];
int j = queries[l][1];
long res = preSum[j + 1] - preSum[i];
int endIdx = Math.min(end[i], j);
res -= (long) (endIdx - i + 1) * idx[i];
ans[l] = res;
}
return ans;
}
}
55 changes: 55 additions & 0 deletions src/main/java/g3701_3800/s3748_count_stable_subarrays/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
3748\. Count Stable Subarrays

Hard

You are given an integer array `nums`.

A ****non-empty subarrays**** 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 <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represents a query. For each query <code>[l<sub>i</sub>, r<sub>i</sub>]</code>, compute the number of **stable subarrays** that lie entirely within the segment <code>nums[l<sub>i</sub>..r<sub>i</sub>]</code>.

Return an integer array `ans` of length `q`, where `ans[i]` is the answer to the <code>i<sup>th</sup></code> 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:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= nums.length - 1</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package g3701_3800.s3748_count_stable_subarrays;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void countStableSubarrays() {
assertThat(
new Solution()
.countStableSubarrays(
new int[] {3, 1, 2}, new int[][] {{0, 1}, {1, 2}, {0, 2}}),
equalTo(new long[] {2L, 3L, 4L}));
}

@Test
void countStableSubarrays2() {
assertThat(
new Solution().countStableSubarrays(new int[] {2, 2}, new int[][] {{0, 1}, {0, 0}}),
equalTo(new long[] {3L, 1L}));
}
}
Loading