Skip to content

644. Maximum Average Subarray II 👍

  • Time: $O(n\log \frac{\max - \min}{10^{-5}})$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
 public:
  double findMaxAverage(vector<int>& nums, int k) {
    constexpr double kErr = 1e-5;
    double l = ranges::min(nums);
    double r = ranges::max(nums);

    while (r - l > kErr) {
      const double m = (l + r) / 2;
      if (check(nums, k, m))
        l = m;
      else
        r = m;
    }

    return l;
  }

 private:
  // Returns true if there's a subarray, where its length >= k and its average
  // sum >= m.
  bool check(const vector<int>& nums, int k, double m) {
    double sum = 0;
    double prevSum = 0;
    double minPrevSum = 0;

    for (int i = 0; i < nums.size(); ++i) {
      // Need to substract m for each `num` so that we can check if the sum of
      // the subarray >= 0.
      sum += nums[i] - m;
      if (i >= k) {
        prevSum += nums[i - k] - m;
        minPrevSum = min(minPrevSum, prevSum);
      }
      if (i + 1 >= k && sum >= minPrevSum)
        return true;
    }

    return false;
  };
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
  public double findMaxAverage(int[] nums, int k) {
    final double kErr = 1e-5;
    double l = (double) Arrays.stream(nums).min().getAsInt();
    double r = (double) Arrays.stream(nums).max().getAsInt();

    while (r - l > kErr) {
      final double m = (l + r) / 2;
      if (check(nums, k, m))
        l = m;
      else
        r = m;
    }

    return l;
  }

  // Returns true if there's a subarray, where its length >= k and its average
  // sum >= m.
  private boolean check(int[] nums, int k, double m) {
    double sum = 0;
    double prevSum = 0;
    double minPrevSum = 0;

    for (int i = 0; i < nums.length; ++i) {
      // Need to substract m for each `num` so that we can check if the sum of
      // the subarray >= 0.
      sum += nums[i] - m;
      if (i >= k) {
        prevSum += nums[i - k] - m;
        minPrevSum = Math.min(minPrevSum, prevSum);
      }
      if (i + 1 >= k && sum >= minPrevSum)
        return true;
    }

    return false;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution:
  def findMaxAverage(self, nums: list[int], k: int) -> float:
    kErr = 1e-5
    l = min(nums)
    r = max(nums)

    def check(m: float) -> bool:
      """
      Returns True if there's a subarray, where its length >= k and its average
      sum >= m.
      """
      summ = 0
      prevSum = 0
      minPrevSum = 0

      for i, num in enumerate(nums):
        # Need to substract m for each `num` so that we can check if the sum of
        # the subarray >= 0.
        summ += num - m
        if i >= k:
          prevSum += nums[i - k] - m
          minPrevSum = min(minPrevSum, prevSum)
        if i + 1 >= k and summ >= minPrevSum:
          return True

      return False

    while r - l > kErr:
      m = (l + r) / 2
      if check(m):
        l = m
      else:
        r = m

    return l