Skip to content

Commit c09c02a

Browse files
committed
+ problem 795
1 parent b4c2cea commit c09c02a

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 795. Number of Subarrays with Bounded Maximum
2+
Given an integer array `nums` and two integers `left` and `right`, return *the number of contiguous non-empty **subarrays** such that the value of the maximum array element in that subarray is in the range* `[left, right]`.
3+
4+
The test cases are generated so that the answer will fit in a **32-bit** integer.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> nums = [2,1,4,3], left = 2, right = 3
9+
<strong>Output:</strong> 3
10+
<strong>Explanation:</strong> There are three subarrays that meet the requirements: [2], [2, 1], [3].
11+
</pre>
12+
13+
#### Example 2:
14+
<pre>
15+
<strong>Input:</strong> nums = [2,9,2,5,6], left = 2, right = 8
16+
<strong>Output:</strong> 7
17+
</pre>
18+
19+
#### Constraints:
20+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
21+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
22+
* <code>0 <= left <= right <= 10<sup>9</sup></code>
23+
24+
## Solutions (Rust)
25+
26+
### 1. Solution
27+
```Rust
28+
impl Solution {
29+
pub fn num_subarray_bounded_max(nums: Vec<i32>, left: i32, right: i32) -> i32 {
30+
let mut desc_stack = vec![];
31+
let mut countl = vec![0; nums.len()];
32+
let mut countr = vec![0; nums.len()];
33+
34+
for i in 0..nums.len() {
35+
while !desc_stack.is_empty() && nums[*desc_stack.last().unwrap() as usize] < nums[i] {
36+
desc_stack.pop();
37+
}
38+
39+
countl[i] = i as i32 - *desc_stack.last().unwrap_or(&-1) - 1;
40+
desc_stack.push(i as i32);
41+
}
42+
desc_stack.clear();
43+
for i in (0..nums.len()).rev() {
44+
while !desc_stack.is_empty() && nums[*desc_stack.last().unwrap() as usize] <= nums[i] {
45+
desc_stack.pop();
46+
}
47+
48+
countr[i] = *desc_stack.last().unwrap_or(&(nums.len() as i32)) - i as i32 - 1;
49+
desc_stack.push(i as i32);
50+
}
51+
52+
(0..nums.len())
53+
.filter(|&i| nums[i] >= left && nums[i] <= right)
54+
.map(|i| (countl[i] + 1) * (countr[i] + 1))
55+
.sum()
56+
}
57+
}
58+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 795. 区间子数组个数
2+
给你一个整数数组 `nums` 和两个整数:`left``right` 。找出 `nums` 中连续、非空且其中最大元素在范围 `[left, right]` 内的子数组,并返回满足条件的子数组的个数。
3+
4+
生成的测试用例保证结果符合 **32-bit** 整数范围。
5+
6+
#### 示例 1:
7+
<pre>
8+
<strong>输入:</strong> nums = [2,1,4,3], left = 2, right = 3
9+
<strong>输出:</strong> 3
10+
<strong>解释:</strong> 满足条件的三个子数组:[2], [2, 1], [3]
11+
</pre>
12+
13+
#### 示例 2:
14+
<pre>
15+
<strong>输入:</strong> nums = [2,9,2,5,6], left = 2, right = 8
16+
<strong>输出:</strong> 7
17+
</pre>
18+
19+
#### 提示:
20+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
21+
* <code>0 <= nums[i] <= 10<sup>9</sup></code>
22+
* <code>0 <= left <= right <= 10<sup>9</sup></code>
23+
24+
## 题解 (Rust)
25+
26+
### 1. 题解
27+
```Rust
28+
impl Solution {
29+
pub fn num_subarray_bounded_max(nums: Vec<i32>, left: i32, right: i32) -> i32 {
30+
let mut desc_stack = vec![];
31+
let mut countl = vec![0; nums.len()];
32+
let mut countr = vec![0; nums.len()];
33+
34+
for i in 0..nums.len() {
35+
while !desc_stack.is_empty() && nums[*desc_stack.last().unwrap() as usize] < nums[i] {
36+
desc_stack.pop();
37+
}
38+
39+
countl[i] = i as i32 - *desc_stack.last().unwrap_or(&-1) - 1;
40+
desc_stack.push(i as i32);
41+
}
42+
desc_stack.clear();
43+
for i in (0..nums.len()).rev() {
44+
while !desc_stack.is_empty() && nums[*desc_stack.last().unwrap() as usize] <= nums[i] {
45+
desc_stack.pop();
46+
}
47+
48+
countr[i] = *desc_stack.last().unwrap_or(&(nums.len() as i32)) - i as i32 - 1;
49+
desc_stack.push(i as i32);
50+
}
51+
52+
(0..nums.len())
53+
.filter(|&i| nums[i] >= left && nums[i] <= right)
54+
.map(|i| (countl[i] + 1) * (countr[i] + 1))
55+
.sum()
56+
}
57+
}
58+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn num_subarray_bounded_max(nums: Vec<i32>, left: i32, right: i32) -> i32 {
3+
let mut desc_stack = vec![];
4+
let mut countl = vec![0; nums.len()];
5+
let mut countr = vec![0; nums.len()];
6+
7+
for i in 0..nums.len() {
8+
while !desc_stack.is_empty() && nums[*desc_stack.last().unwrap() as usize] < nums[i] {
9+
desc_stack.pop();
10+
}
11+
12+
countl[i] = i as i32 - *desc_stack.last().unwrap_or(&-1) - 1;
13+
desc_stack.push(i as i32);
14+
}
15+
desc_stack.clear();
16+
for i in (0..nums.len()).rev() {
17+
while !desc_stack.is_empty() && nums[*desc_stack.last().unwrap() as usize] <= nums[i] {
18+
desc_stack.pop();
19+
}
20+
21+
countr[i] = *desc_stack.last().unwrap_or(&(nums.len() as i32)) - i as i32 - 1;
22+
desc_stack.push(i as i32);
23+
}
24+
25+
(0..nums.len())
26+
.filter(|&i| nums[i] >= left && nums[i] <= right)
27+
.map(|i| (countl[i] + 1) * (countr[i] + 1))
28+
.sum()
29+
}
30+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@
530530
[791][791l] |[Custom Sort String][791] |![rs]
531531
[792][792l] |[Number of Matching Subsequences][792] |![rs]
532532
[794][794l] |[Valid Tic-Tac-Toe State][794] |![rs]
533+
[795][795l] |[Number of Subarrays with Bounded Maximum][795] |![rs]
533534
[796][796l] |[Rotate String][796] |![rs]
534535
[797][797l] |[All Paths From Source to Target][797] |![py]
535536
[798][798l] |[Smallest Rotation with Highest Score][798] |![rs]
@@ -2156,6 +2157,7 @@
21562157
[791]:Problemset/0791-Custom%20Sort%20String/README.md#791-custom-sort-string
21572158
[792]:Problemset/0792-Number%20of%20Matching%20Subsequences/README.md#792-number-of-matching-subsequences
21582159
[794]:Problemset/0794-Valid%20Tic-Tac-Toe%20State/README.md#794-valid-tic-tac-toe-state
2160+
[795]:Problemset/0795-Number%20of%20Subarrays%20with%20Bounded%20Maximum/README.md#795-number-of-subarrays-with-bounded-maximum
21592161
[796]:Problemset/0796-Rotate%20String/README.md#796-rotate-string
21602162
[797]:Problemset/0797-All%20Paths%20From%20Source%20to%20Target/README.md#797-all-paths-from-source-to-target
21612163
[798]:Problemset/0798-Smallest%20Rotation%20with%20Highest%20Score/README.md#798-smallest-rotation-with-highest-score
@@ -3776,6 +3778,7 @@
37763778
[791l]:https://leetcode.com/problems/custom-sort-string/
37773779
[792l]:https://leetcode.com/problems/number-of-matching-subsequences/
37783780
[794l]:https://leetcode.com/problems/valid-tic-tac-toe-state/
3781+
[795l]:https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/
37793782
[796l]:https://leetcode.com/problems/rotate-string/
37803783
[797l]:https://leetcode.com/problems/all-paths-from-source-to-target/
37813784
[798l]:https://leetcode.com/problems/smallest-rotation-with-highest-score/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@
530530
[791][791l] |[自定义字符串排序][791] |![rs]
531531
[792][792l] |[匹配子序列的单词数][792] |![rs]
532532
[794][794l] |[有效的井字游戏][794] |![rs]
533+
[795][795l] |[区间子数组个数][795] |![rs]
533534
[796][796l] |[旋转字符串][796] |![rs]
534535
[797][797l] |[所有可能的路径][797] |![py]
535536
[798][798l] |[得分最高的最小轮调][798] |![rs]
@@ -2156,6 +2157,7 @@
21562157
[791]:Problemset/0791-Custom%20Sort%20String/README_CN.md#791-自定义字符串排序
21572158
[792]:Problemset/0792-Number%20of%20Matching%20Subsequences/README_CN.md#792-匹配子序列的单词数
21582159
[794]:Problemset/0794-Valid%20Tic-Tac-Toe%20State/README_CN.md#794-有效的井字游戏
2160+
[795]:Problemset/0795-Number%20of%20Subarrays%20with%20Bounded%20Maximum/README_CN.md#795-区间子数组个数
21592161
[796]:Problemset/0796-Rotate%20String/README_CN.md#796-旋转字符串
21602162
[797]:Problemset/0797-All%20Paths%20From%20Source%20to%20Target/README_CN.md#797-所有可能的路径
21612163
[798]:Problemset/0798-Smallest%20Rotation%20with%20Highest%20Score/README_CN.md#798-得分最高的最小轮调
@@ -3776,6 +3778,7 @@
37763778
[791l]:https://leetcode.cn/problems/custom-sort-string/
37773779
[792l]:https://leetcode.cn/problems/number-of-matching-subsequences/
37783780
[794l]:https://leetcode.cn/problems/valid-tic-tac-toe-state/
3781+
[795l]:https://leetcode.cn/problems/number-of-subarrays-with-bounded-maximum/
37793782
[796l]:https://leetcode.cn/problems/rotate-string/
37803783
[797l]:https://leetcode.cn/problems/all-paths-from-source-to-target/
37813784
[798l]:https://leetcode.cn/problems/smallest-rotation-with-highest-score/

0 commit comments

Comments
 (0)