Skip to content

Commit 4d91e41

Browse files
committed
Add solution 0795
1 parent 7a90f37 commit 4d91e41

26 files changed

+955
-777
lines changed

README.md

Lines changed: 575 additions & 575 deletions
Large diffs are not rendered by default.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
func numSubarrayBoundedMax(nums []int, left int, right int) int {
4+
return getAnswerPerBound(nums, right) - getAnswerPerBound(nums, left-1)
5+
}
6+
7+
func getAnswerPerBound(nums []int, bound int) int {
8+
res, count := 0, 0
9+
for _, num := range nums {
10+
if num <= bound {
11+
count++
12+
} else {
13+
count = 0
14+
}
15+
res += count
16+
}
17+
return res
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question795 struct {
9+
para795
10+
ans795
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para795 struct {
16+
nums []int
17+
left int
18+
right int
19+
}
20+
21+
// ans 是答案
22+
// one 代表第一个答案
23+
type ans795 struct {
24+
one int
25+
}
26+
27+
func Test_Problem795(t *testing.T) {
28+
29+
qs := []question795{
30+
31+
{
32+
para795{[]int{2, 1, 4, 3}, 2, 3},
33+
ans795{3},
34+
},
35+
}
36+
37+
fmt.Printf("------------------------Leetcode Problem 795------------------------\n")
38+
39+
for _, q := range qs {
40+
_, p := q.ans795, q.para795
41+
fmt.Printf("【input】:%v 【output】:%v\n", p, numSubarrayBoundedMax(p.nums, p.left, p.right))
42+
}
43+
fmt.Printf("\n\n\n")
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [795. Number of Subarrays with Bounded Maximum](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)
2+
3+
4+
## 题目
5+
6+
We are given an array `nums` of positive integers, and two positive integers `left` and `right` (`left <= right`).
7+
8+
Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least `left` and at most `right`.
9+
10+
```
11+
Example:Input:
12+
nums = [2, 1, 4, 3]
13+
left = 2
14+
right = 3
15+
Output: 3
16+
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].
17+
```
18+
19+
**Note:**
20+
21+
- `left``right`, and `nums[i]` will be an integer in the range `[0, 109]`.
22+
- The length of `nums` will be in the range of `[1, 50000]`.
23+
24+
## 题目大意
25+
26+
给定一个元素都是正整数的数组`A` ,正整数 `L` 以及 `R` (`L <= R`)。求连续、非空且其中最大元素满足大于等于`L` 小于等于`R`的子数组个数。
27+
28+
## 解题思路
29+
30+
- 题目要求子数组最大元素在 [L,R] 区间内。假设 count(bound) 为计算所有元素都小于等于 bound 的子数组数量。那么本题所求的答案可转化为 count(R) - count(L-1)。
31+
- 如何统计所有元素小于 bound 的子数组数量呢?使用 count 变量记录在 bound 的左边,小于等于 bound 的连续元素数量。当找到一个这样的元素时,在此位置上结束的有效子数组的数量为 count + 1。当遇到一个元素大于 B 时,则在此位置结束的有效子数组的数量为 0。res 将每轮 count 累加,最终 res 中存的即是满足条件的所有子数组数量。
32+
33+
## 代码
34+
35+
```go
36+
package leetcode
37+
38+
func numSubarrayBoundedMax(nums []int, left int, right int) int {
39+
return getAnswerPerBound(nums, right) - getAnswerPerBound(nums, left-1)
40+
}
41+
42+
func getAnswerPerBound(nums []int, bound int) int {
43+
res, count := 0, 0
44+
for _, num := range nums {
45+
if num <= bound {
46+
count++
47+
} else {
48+
count = 0
49+
}
50+
res += count
51+
}
52+
return res
53+
}
54+
```

website/content/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,5 @@ func preimageSizeFZF1(K int) int {
104104
----------------------------------------------
105105
<div style="display: flex;justify-content: space-between;align-items: center;">
106106
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction/">⬅️上一页</a></p>
107-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0802.Find-Eventual-Safe-States/">下一页➡️</a></p>
107+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum/">下一页➡️</a></p>
108108
</div>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# [795. Number of Subarrays with Bounded Maximum](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/)
2+
3+
4+
## 题目
5+
6+
We are given an array `nums` of positive integers, and two positive integers `left` and `right` (`left <= right`).
7+
8+
Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least `left` and at most `right`.
9+
10+
```
11+
Example:Input:
12+
nums = [2, 1, 4, 3]
13+
left = 2
14+
right = 3
15+
Output: 3
16+
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].
17+
```
18+
19+
**Note:**
20+
21+
- `left``right`, and `nums[i]` will be an integer in the range `[0, 109]`.
22+
- The length of `nums` will be in the range of `[1, 50000]`.
23+
24+
## 题目大意
25+
26+
给定一个元素都是正整数的数组`A` ,正整数 `L` 以及 `R` (`L <= R`)。求连续、非空且其中最大元素满足大于等于`L` 小于等于`R`的子数组个数。
27+
28+
## 解题思路
29+
30+
- 题目要求子数组最大元素在 [L,R] 区间内。假设 count(bound) 为计算所有元素都小于等于 bound 的子数组数量。那么本题所求的答案可转化为 count(R) - count(L-1)。
31+
- 如何统计所有元素小于 bound 的子数组数量呢?使用 count 变量记录在 bound 的左边,小于等于 bound 的连续元素数量。当找到一个这样的元素时,在此位置上结束的有效子数组的数量为 count + 1。当遇到一个元素大于 B 时,则在此位置结束的有效子数组的数量为 0。res 将每轮 count 累加,最终 res 中存的即是满足条件的所有子数组数量。
32+
33+
## 代码
34+
35+
```go
36+
package leetcode
37+
38+
func numSubarrayBoundedMax(nums []int, left int, right int) int {
39+
return getAnswerPerBound(nums, right) - getAnswerPerBound(nums, left-1)
40+
}
41+
42+
func getAnswerPerBound(nums []int, bound int) int {
43+
res, count := 0, 0
44+
for _, num := range nums {
45+
if num <= bound {
46+
count++
47+
} else {
48+
count = 0
49+
}
50+
res += count
51+
}
52+
return res
53+
}
54+
```
55+
56+
57+
----------------------------------------------
58+
<div style="display: flex;justify-content: space-between;align-items: center;">
59+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">⬅️上一页</a></p>
60+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0802.Find-Eventual-Safe-States/">下一页➡️</a></p>
61+
</div>

website/content/ChapterFour/0800~0899/0802.Find-Eventual-Safe-States.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,6 @@ func dfsEventualSafeNodes(graph [][]int, idx int, color []int) bool {
7575

7676
----------------------------------------------
7777
<div style="display: flex;justify-content: space-between;align-items: center;">
78-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">⬅️上一页</a></p>
78+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum/">⬅️上一页</a></p>
7979
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0803.Bricks-Falling-When-Hit/">下一页➡️</a></p>
8080
</div>

0 commit comments

Comments
 (0)