Skip to content

Commit 1c40cc0

Browse files
committed
Added task 3748
1 parent dd7709a commit 1c40cc0

3 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g3701_3800.s3748_count_stable_subarrays;
2+
3+
// #Hard #Array #Binary_Search #Prefix_Sum #Senior_Staff #Weekly_Contest_476
4+
// #2026_04_26_Time_9_ms_(100.00%)_Space_183.68_MB_(43.30%)
5+
6+
public class Solution {
7+
public long[] countStableSubarrays(int[] nums, int[][] queries) {
8+
int n = nums.length;
9+
long[] preSum = new long[n + 1];
10+
int[] idx = new int[n];
11+
int[] end = new int[n];
12+
int cnt = 0;
13+
int prv = -1;
14+
for (int i = 0; i < n; i++) {
15+
if (nums[i] >= prv) {
16+
cnt++;
17+
} else {
18+
cnt = 1;
19+
}
20+
prv = nums[i];
21+
preSum[i + 1] = preSum[i] + cnt;
22+
idx[i] = cnt - 1;
23+
}
24+
end[n - 1] = n - 1;
25+
for (int i = n - 2; i >= 0; i--) {
26+
if (idx[i] + 1 == idx[i + 1]) {
27+
end[i] = end[i + 1];
28+
} else {
29+
end[i] = i;
30+
}
31+
}
32+
long[] ans = new long[queries.length];
33+
for (int l = 0; l < queries.length; l++) {
34+
int i = queries[l][0];
35+
int j = queries[l][1];
36+
long res = preSum[j + 1] - preSum[i];
37+
int endIdx = Math.min(end[i], j);
38+
res -= (long) (endIdx - i + 1) * idx[i];
39+
ans[l] = res;
40+
}
41+
return ans;
42+
}
43+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
3748\. Count Stable Subarrays
2+
3+
Hard
4+
5+
You are given an integer array `nums`.
6+
7+
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]`.
8+
9+
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>.
10+
11+
Return an integer array `ans` of length `q`, where `ans[i]` is the answer to the <code>i<sup>th</sup></code> query.
12+
13+
**Note**:
14+
15+
* A single element subarray is considered stable.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [3,1,2], queries = [[0,1],[1,2],[0,2]]
20+
21+
**Output:** [2,3,4]
22+
23+
**Explanation:**
24+
25+
* For `queries[0] = [0, 1]`, the subarray is `[nums[0], nums[1]] = [3, 1]`.
26+
* The stable subarrays are `[3]` and `[1]`. The total number of stable subarrays is 2.
27+
* For `queries[1] = [1, 2]`, the subarray is `[nums[1], nums[2]] = [1, 2]`.
28+
* The stable subarrays are `[1]`, `[2]`, and `[1, 2]`. The total number of stable subarrays is 3.
29+
* For `queries[2] = [0, 2]`, the subarray is `[nums[0], nums[1], nums[2]] = [3, 1, 2]`.
30+
* The stable subarrays are `[3]`, `[1]`, `[2]`, and `[1, 2]`. The total number of stable subarrays is 4.
31+
32+
Thus, `ans = [2, 3, 4]`.
33+
34+
**Example 2:**
35+
36+
**Input:** nums = [2,2], queries = [[0,1],[0,0]]
37+
38+
**Output:** [3,1]
39+
40+
**Explanation:**
41+
42+
* For `queries[0] = [0, 1]`, the subarray is `[nums[0], nums[1]] = [2, 2]`.
43+
* The stable subarrays are `[2]`, `[2]`, and `[2, 2]`. The total number of stable subarrays is 3.
44+
* For `queries[1] = [0, 0]`, the subarray is `[nums[0]] = [2]`.
45+
* The stable subarray is `[2]`. The total number of stable subarrays is 1.
46+
47+
Thus, `ans = [3, 1]`.
48+
49+
**Constraints:**
50+
51+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
52+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
53+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
54+
* <code>queries[i] = [l<sub>i</sub>, r<sub>i</sub>]</code>
55+
* <code>0 <= l<sub>i</sub> <= r<sub>i</sub> <= nums.length - 1</code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3701_3800.s3748_count_stable_subarrays;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countStableSubarrays() {
11+
assertThat(
12+
new Solution()
13+
.countStableSubarrays(
14+
new int[] {3, 1, 2}, new int[][] {{0, 1}, {1, 2}, {0, 2}}),
15+
equalTo(new long[] {2L, 3L, 4L}));
16+
}
17+
18+
@Test
19+
void countStableSubarrays2() {
20+
assertThat(
21+
new Solution().countStableSubarrays(new int[] {2, 2}, new int[][] {{0, 1}, {0, 0}}),
22+
equalTo(new long[] {3L, 1L}));
23+
}
24+
}

0 commit comments

Comments
 (0)