From 420782bd9bf4e97f424738a669ab274e3c3d3df4 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 21 Oct 2023 15:55:02 +0800 Subject: [PATCH] Add solution and test-cases for problem 1343 --- .../README.md | 23 ++++++++----------- .../Solution.go | 22 ++++++++++++++++-- .../Solution_test.go | 22 +++++++++--------- 3 files changed, 40 insertions(+), 27 deletions(-) diff --git a/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/README.md b/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/README.md index 590d4a9ca..9a6d3c4fe 100644 --- a/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/README.md +++ b/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/Solution.go b/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/Solution.go index d115ccf5e..cb92063b6 100644 --- a/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/Solution.go +++ b/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/Solution.go @@ -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 } diff --git a/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/Solution_test.go b/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/Solution_test.go index 14ff50eb4..b4edab660 100644 --- a/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/Solution_test.go +++ b/leetcode/1301-1400/1343.Number-of-Sub-arrays-of-Size-K-and-Average-Greater-than-or-Equal-to-Threshold/Solution_test.go @@ -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() { }