|
|
@@ -0,0 +1,35 @@
|
|
|
+package leetcode.p643;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ProjectName: LeetCode
|
|
|
+ * @FileName: Solution
|
|
|
+ * @Author: 杨逸
|
|
|
+ * @Data:2025/8/13 13:13
|
|
|
+ * @Description: https://leetcode.cn/problems/maximum-average-subarray-i/description/
|
|
|
+ * 643. 子数组最大平均数 I
|
|
|
+ */
|
|
|
+class Solution {
|
|
|
+ public double findMaxAverage(int[] nums, int k) {
|
|
|
+ int left = 0;
|
|
|
+ int right = 0;
|
|
|
+ //Double.MIN_VALUE取的最小浮点数是一个正的很小的数不是负数,取浮点数最小值应使用 -Double.MAX_VALUE
|
|
|
+ double maxAvg = - Double.MAX_VALUE;
|
|
|
+ double currentValue = 0;
|
|
|
+ double temp;
|
|
|
+ while (right < nums.length){
|
|
|
+ if(right - left == k){
|
|
|
+ temp = currentValue / k;
|
|
|
+ if(temp > maxAvg){
|
|
|
+ maxAvg = temp;
|
|
|
+ }
|
|
|
+ currentValue -= nums[left++];
|
|
|
+ }
|
|
|
+ currentValue += nums[right++];
|
|
|
+ }
|
|
|
+ temp = currentValue / k;
|
|
|
+ if (temp > maxAvg){
|
|
|
+ maxAvg = temp;
|
|
|
+ }
|
|
|
+ return maxAvg;
|
|
|
+ }
|
|
|
+}
|