diff --git a/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/README.md b/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/README.md index eb73d67e7..bf694120d 100644 --- a/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/README.md +++ b/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution.go b/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution.go index d115ccf5e..6990b584f 100644 --- a/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution.go +++ b/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution.go @@ -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 } diff --git a/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution_test.go b/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution_test.go index 14ff50eb4..063eb7eb2 100644 --- a/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution_test.go +++ b/leetcode/701-800/0795.Number-of-Subarrays-with-Bounded-Maximum/Solution_test.go @@ -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() { }