From fa5b9e2c915627c72f75ef1babe3bd00e1d2c13c Mon Sep 17 00:00:00 2001 From: ductnn Date: Mon, 11 Dec 2023 21:54:46 +0700 Subject: [PATCH] update --- .../maximumAverageSubarrayI.go | 54 ------------------- 1 file changed, 54 deletions(-) delete mode 100644 leetcode/643.MaximumAverageSubarrayI/maximumAverageSubarrayI.go diff --git a/leetcode/643.MaximumAverageSubarrayI/maximumAverageSubarrayI.go b/leetcode/643.MaximumAverageSubarrayI/maximumAverageSubarrayI.go deleted file mode 100644 index a571cf3..0000000 --- a/leetcode/643.MaximumAverageSubarrayI/maximumAverageSubarrayI.go +++ /dev/null @@ -1,54 +0,0 @@ -package main - -import ( - "fmt" -) - -func findMaxAverage(nums []int, k int) float64 { - sum := 0 - for i := 0; i < k; i++ { - sum += nums[i] - } - - result := sum - for i := k; i < len(nums); i++ { - sum += nums[i] - nums[i-k] - if result < sum { - result = sum - } - } - - return float64(result) / float64(k) -} - -func findMaxAverage1(nums []int, k int) float64 { - j := 0 - res := average(nums[:k]) - for i := k; i <= len(nums); i++ { - res = max(res, average(nums[j:i])) - j++ - } - return res -} - -func average(temp []int) float64 { - sum := 0 - for i := range temp { - sum += temp[i] - } - return float64(sum) / float64(len(temp)) -} - -func max(a, b float64) float64 { - if a < b { - return b - } - return a -} - -func main() { - nums := []int{1, 12, -5, -6, 50, 3} - k := 4 - - fmt.Println(findMaxAverage(nums, k)) -}