Skip to content

Commit 2e1b3da

Browse files
committed
Add solution and test-cases for problem 795
1 parent 09a1dcb commit 2e1b3da

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
# [795.Number of Subarrays with Bounded Maximum][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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]`.
5+
6+
The test cases are generated so that the answer will fit in a **32-bit** integer.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: nums = [2,1,4,3], left = 2, right = 3
12+
Output: 3
13+
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].
1314
```
1415

15-
## 题意
16-
> ...
17-
18-
## 题解
16+
**Example 2:**
1917

20-
### 思路1
21-
> ...
22-
Number of Subarrays with Bounded Maximum
23-
```go
2418
```
25-
19+
Input: nums = [2,9,2,5,6], left = 2, right = 8
20+
Output: 7
21+
```
2622

2723
## 结语
2824

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, left int, right int) int {
4+
ans := 0
5+
// 1, 2, 2, 1 [2,3]
6+
// 2[1,2], [2]
7+
// 2 [2], [2,2], [1,2,2], | [2,2,1], [1,2,2,1], [2,1]
8+
//
9+
for i := range nums {
10+
if nums[i] >= left && nums[i] <= right {
11+
li := i
12+
for ; li >= 0 && nums[li] <= nums[i]; li-- {
13+
}
14+
left := i - li
15+
ans += left
16+
17+
ri := i + 1
18+
for ; ri < len(nums) && nums[ri] < nums[i]; ri++ {
19+
}
20+
ri--
21+
if diff := ri - i; diff > 0 {
22+
ans += diff * left
23+
}
24+
}
25+
}
26+
return ans
527
}

leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
nums []int
14+
left, right int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 1, 4, 3}, 2, 3, 3},
18+
{"TestCase2", []int{2, 9, 2, 5, 6}, 2, 8, 7},
19+
{"TestCase3", []int{1, 2, 2, 1}, 2, 3, 8},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.nums, c.left, c.right)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.nums, c.left, c.right)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)