Skip to content

Commit b68f5a5

Browse files
committed
added tests for findMaxAverage
1 parent 3fef175 commit b68f5a5

2 files changed

Lines changed: 30 additions & 7 deletions

File tree

Go/Leetcode/find_max_average/find_max_average_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ package find_max_average
22

33
import "testing"
44

5-
func TestSolve(t *testing.T) {
6-
t.Fatal("write tests")
5+
func TestFindMaxAverage(t *testing.T) {
6+
cases := []struct {
7+
nums []int
8+
k int
9+
out float64
10+
}{
11+
{[]int{1, 12, -5, -6, 50, 3}, 4, 12.75000},
12+
{[]int{5}, 1, 5.00000},
13+
{[]int{-1}, 1, -1.00000},
14+
}
15+
for _, c := range cases {
16+
res := findMaxAverage(c.nums, c.k)
17+
if res != c.out {
18+
t.Errorf("findMaxAverage(%v, %v)=%v, but expected %v", c.nums, c.k, res, c.out)
19+
}
20+
}
721
}

Go/Leetcode/find_max_average/q.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
# find_max_average
1+
# 643. Maximum Average Subarray I
22

33
## Problem
44

5-
Paste the problem description here.
6-
5+
You are given an integer array nums consisting of n elements, and an integer k.
6+
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted
77
## Examples
8+
Example 1:
9+
Input: nums = [1,12,-5,-6,50,3], k = 4
10+
Output: 12.75000
11+
Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
12+
13+
Example 2:
14+
Input: nums = [5], k = 1
15+
Output: 5.00000
816

917
## Notes
1018
- Constraints:
11-
- Edge cases:
12-
- Complexity:
19+
n == nums.length
20+
1 <= k <= n <= 105
21+
-104 <= nums[i] <= 104

0 commit comments

Comments
 (0)