Skip to content

Commit e3ea188

Browse files
authored
Merge pull request #869 from 0xff-dev/2302
Add solution and test-cases for problem 2302
2 parents 5fce35a + 3801048 commit e3ea188

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

leetcode/2301-2400/2302.Count-Subarrays-With-Score-Less-Than-K/README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [2302.Count Subarrays With Score Less Than K][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+
The **score** of an array is defined as the **product** of its sum and its length.
5+
6+
- For example, the score of `[1, 2, 3, 4, 5]` is `(1 + 2 + 3 + 4 + 5) * 5 = 75`.
7+
8+
Given a positive integer array `nums` and an integer `k`, return the **number of non-empty subarrays** of `nums` whose score is **strictly less** than `k`.
9+
10+
A **subarray** is a contiguous sequence of elements within an array.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: nums = [2,1,4,3,5], k = 10
16+
Output: 6
17+
Explanation:
18+
The 6 subarrays having scores less than 10 are:
19+
- [2] with score 2 * 1 = 2.
20+
- [1] with score 1 * 1 = 1.
21+
- [4] with score 4 * 1 = 4.
22+
- [3] with score 3 * 1 = 3.
23+
- [5] with score 5 * 1 = 5.
24+
- [2,1] with score (2 + 1) * 2 = 6.
25+
Note that subarrays such as [1,4] and [4,3,5] are not considered because their scores are 10 and 36 respectively, while we need scores strictly less than 10.
1326
```
1427

15-
## 题意
16-
> ...
28+
**Example 2:**
1729

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Count Subarrays With Score Less Than K
23-
```go
2430
```
25-
31+
Input: nums = [1,1,1], k = 5
32+
Output: 5
33+
Explanation:
34+
Every subarray except [1,1,1] has a score less than 5.
35+
[1,1,1] has a score (1 + 1 + 1) * 3 = 9, which is greater than 5.
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(nums []int, k int64) int64 {
6+
ans := int64(0)
7+
for j := 1; j < len(nums); j++ {
8+
nums[j] += nums[j-1]
9+
}
10+
for j := 0; j < len(nums); j++ {
11+
l := j + 1
12+
idx := sort.Search(l, func(i int) bool {
13+
width := l - i
14+
del := 0
15+
if i > 0 {
16+
del = nums[i-1]
17+
}
18+
return int64(nums[j]-del)*int64(width) < k
19+
})
20+
if idx != l {
21+
ans += int64(l - idx)
22+
}
23+
}
24+
return ans
525
}

leetcode/2301-2400/2302.Count-Subarrays-With-Score-Less-Than-K/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
nums []int
14+
k int64
15+
expect int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 1, 4, 3, 5}, 10, 6},
18+
{"TestCase2", []int{1, 1, 1}, 5, 5},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.nums, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.nums, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)