Skip to content
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

Add solution and test-cases for problem 1343 #666

Merged
merged 1 commit into from
Oct 28, 2023
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,23 @@
# [1343.Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold][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 array of integers `arr` and two integers k and `threshold`, return the number of sub-arrays of size `k` and average greater than or equal to `threshold`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4
Output: 3
Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold).
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
```go
```

Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5
Output: 6
Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(arr []int, k int, threshold int) int {
ans := 0
sum := 0
for i := 0; i < k; i++ {
sum += arr[i]
}
if sum/k >= threshold {
ans++
}
start := 0
// 1, 2, 3
for i := k; i < len(arr); i++ {
sum -= arr[start]
sum += arr[i]
if sum/k >= threshold {
ans++
}
start++
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
inputs []int
k, threshold int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{2, 2, 2, 2, 5, 5, 5, 8}, 3, 4, 3},
{"TestCase2", []int{11, 13, 17, 23, 29, 31, 7, 5, 2, 3}, 3, 5, 6},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k, c.threshold)
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.inputs, c.k, c.threshold)
}
})
}
}

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

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