Skip to content

Add solution and test-cases for problem 795 #821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
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
@@ -1,28 +1,24 @@
# [795.Number of Subarrays with Bounded Maximum][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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]`.

The test cases are generated so that the answer will fit in a **32-bit** integer.

**Example 1:**

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

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Number of Subarrays with Bounded Maximum
```go
```

Input: nums = [2,9,2,5,6], left = 2, right = 8
Output: 7
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums []int, left int, right int) int {
ans := 0
// 1, 2, 2, 1 [2,3]
// 2[1,2], [2]
// 2 [2], [2,2], [1,2,2], | [2,2,1], [1,2,2,1], [2,1]
//
for i := range nums {
if nums[i] >= left && nums[i] <= right {
li := i
for ; li >= 0 && nums[li] <= nums[i]; li-- {
}
left := i - li
ans += left

ri := i + 1
for ; ri < len(nums) && nums[ri] < nums[i]; ri++ {
}
ri--
if diff := ri - i; diff > 0 {
ans += diff * left
}
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
nums []int
left, right int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{2, 1, 4, 3}, 2, 3, 3},
{"TestCase2", []int{2, 9, 2, 5, 6}, 2, 8, 7},
{"TestCase3", []int{1, 2, 2, 1}, 2, 3, 8},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.nums, c.left, c.right)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.nums, c.left, c.right)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}