8000 Merge pull request #138 from kylesliu/develop · deardeng/awesome-golang-leetcode@ded32f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit ded32f9

Browse files
author
Kyle Liu
authored
Merge pull request 6boris#138 from kylesliu/develop
add 643 solution
2 parents 118d9b1 + add572e commit ded32f9

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func findMaxAverage(nums []int, k int) float64 {
4+
tmp, ans := 0, -1
5+
for i := 0; i < len(nums); i++ {
6+
tmp += nums[i]
7+
if i >= k-1 {
8+
ans = max(ans, tmp)
9+
tmp -= nums[i-k+1]
10+
}
11+
}
12+
13+
return float64(ans) / float64(k)
14+
}
15+
16+
func max(x, y int) int {
17+
if x > y {
18+
return x
19+
}
20+
return y
521
}

src/0643.Maximum-Average-Subarray-I/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
input1 []int
14+
input2 int
15+
expect float64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", []int{1, 12, -5, -6, 50, 3}, 4, 12.75},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := findMaxAverage(c.input1, c.input2)
2524
if !reflect.DeepEqual(got, c.expect) {
2625
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
c.expect, got, c.input1)
2827
}
2928
})
3029
}

0 commit comments

Comments
 (0)
0