You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0600-0699/0643.Maximum Average Subarray I/README_EN.md
+54-24
Original file line number
Diff line number
Diff line change
@@ -37,15 +37,18 @@
37
37
38
38
## Solutions
39
39
40
-
### Solution 1
40
+
### Solution 1: Sliding Window
41
+
42
+
We maintain a sliding window of length $k$, and for each window, we calculate the sum $s$ of the numbers within the window. We take the maximum sum $s$ as the answer.
43
+
44
+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
Copy file name to clipboardexpand all lines: solution/0600-0699/0644.Maximum Average Subarray II/README_EN.md
+35-1
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,41 @@ Note that we do not consider the subarrays of length < 4.
42
42
43
43
## Solutions
44
44
45
-
### Solution 1
45
+
### Solution 1: Binary Search
46
+
47
+
We note that if the average value of a subarray with length greater than or equal to $k$ is $v$, then the maximum average number must be greater than or equal to $v$, otherwise the maximum average number must be less than $v$. Therefore, we can use binary search to find the maximum average number.
48
+
49
+
What are the left and right boundaries of binary search? The left boundary $l$ must be the minimum value in the array, and the right boundary $r$ is the maximum value in the array. Next, we binary search the midpoint $mid$, and judge whether there exists a subarray with length greater than or equal to $k$ whose average value is greater than or equal to $mid$. If it exists, then we update the left boundary $l$ to $mid$, otherwise we update the right boundary $r$ to $mid$. When the difference between the left and right boundaries is less than a very small non-negative number, i.e., $r - l < \epsilon$, we can get the maximum average number, where $\epsilon$ represents a very small positive number, which can be $10^{-5}$.
50
+
51
+
The key to the problem is how to judge whether the average value of a subarray with length greater than or equal to $k$ is greater than or equal to $v$.
52
+
53
+
We assume that in the array $nums$, there is a subarray with length $j$, the elements are $a_1, a_2, \cdots, a_j$, and its average value is greater than or equal to $v$, i.e.,
54
+
55
+
$$
56
+
\frac{a_1 + a_2 + \cdots + a_j}{j} \geq v
57
+
$$
58
+
59
+
Then,
60
+
61
+
$$
62
+
a_1 + a_2 + \cdots + a_j \geq v \times j
63
+
$$
64
+
65
+
That is,
66
+
67
+
$$
68
+
(a_1 - v) + (a_2 - v) + \cdots + (a_j - v) \geq 0
69
+
$$
70
+
71
+
We can find that if we subtract $v$ from each element in the array $nums$, the original problem is transformed into a problem of whether the sum of the elements of a subarray with length greater than or equal to $k$ is greater than or equal to $0$. We can use a sliding window to solve this problem.
72
+
73
+
First, we calculate the sum $s$ of the differences between the first $k$ elements and $v$. If $s \geq 0$, it means that there exists a subarray with length greater than or equal to $k$ whose element sum is greater than or equal to $0$.
74
+
75
+
Otherwise, we continue to traverse the element $nums[j]$. Suppose the current sum of the differences between the first $j$ elements and $v$ is $s_j$. Then we can maintain the minimum value $mi$ of the sum of the differences between the prefix sum and $v$ in the range $[0,..j-k]$. If $s_j \geq mi$ exists, it means that there exists a subarray with length greater than or equal to $k$ whose element sum is greater than or equal to $0$, and we return $true$.
76
+
77
+
Otherwise, we continue to traverse the element $nums[j]$ until the entire array is traversed.
78
+
79
+
The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the length of the array $nums$ and the difference between the maximum and minimum values in the array, respectively. The space complexity is $O(1)$.
0 commit comments