Skip to content

Commit 463a28c

Browse files
authored
feat: add solutions to lc problems: No.0643,1984 (#2556)
* No.0643.Maximum Average Subarray I * No.1984.Minimum Difference Between Highest and Lowest of K Scores
1 parent 2ec0d28 commit 463a28c

File tree

13 files changed

+216
-87
lines changed

13 files changed

+216
-87
lines changed

solution/0600-0699/0643.Maximum Average Subarray I/README.md

+54-24
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@
4343

4444
## 解法
4545

46-
### 方法一
46+
### 方法一:滑动窗口
47+
48+
我们维护一个长度为 $k$ 的滑动窗口,每次计算窗口内的和 $s$,取最大的和 $s$ 作为答案。
49+
50+
时间复杂度 $O(n)$,其中 $n$ 是数组 $nums$ 的长度。空间复杂度 $O(1)$。
4751

4852
<!-- tabs:start -->
4953

5054
```python
5155
class Solution:
5256
def findMaxAverage(self, nums: List[int], k: int) -> float:
53-
s = sum(nums[:k])
54-
ans = s
57+
ans = s = sum(nums[:k])
5558
for i in range(k, len(nums)):
5659
s += nums[i] - nums[i - k]
5760
ans = max(ans, s)
@@ -75,19 +78,46 @@ class Solution {
7578
}
7679
```
7780

81+
```cpp
82+
class Solution {
83+
public:
84+
double findMaxAverage(vector<int>& nums, int k) {
85+
int s = accumulate(nums.begin(), nums.begin() + k, 0);
86+
int ans = s;
87+
for (int i = k; i < nums.size(); ++i) {
88+
s += nums[i] - nums[i - k];
89+
ans = max(ans, s);
90+
}
91+
return static_cast<double>(ans) / k;
92+
}
93+
};
94+
```
95+
96+
```go
97+
func findMaxAverage(nums []int, k int) float64 {
98+
s := 0
99+
for _, x := range nums[:k] {
100+
s += x
101+
}
102+
ans := s
103+
for i := k; i < len(nums); i++ {
104+
s += nums[i] - nums[i-k]
105+
ans = max(ans, s)
106+
}
107+
return float64(ans) / float64(k)
108+
}
109+
```
110+
78111
```ts
79112
function findMaxAverage(nums: number[], k: number): number {
80-
let n = nums.length;
81-
let ans = 0;
82-
let sum = 0;
83-
// 前k
84-
for (let i = 0; i < k; i++) {
85-
sum += nums[i];
113+
let s = 0;
114+
for (let i = 0; i < k; ++i) {
115+
s += nums[i];
86116
}
87-
ans = sum;
88-
for (let i = k; i < n; i++) {
89-
sum += nums[i] - nums[i - k];
90-
ans = Math.max(ans, sum);
117+
let ans = s;
118+
for (let i = k; i < nums.length; ++i) {
119+
s += nums[i] - nums[i - k];
120+
ans = Math.max(ans, s);
91121
}
92122
return ans / k;
93123
}
@@ -98,13 +128,13 @@ impl Solution {
98128
pub fn find_max_average(nums: Vec<i32>, k: i32) -> f64 {
99129
let k = k as usize;
100130
let n = nums.len();
101-
let mut sum = nums.iter().take(k).sum::<i32>();
102-
let mut max = sum;
131+
let mut s = nums.iter().take(k).sum::<i32>();
132+
let mut ans = s;
103133
for i in k..n {
104-
sum += nums[i] - nums[i - k];
105-
max = max.max(sum);
134+
s += nums[i] - nums[i - k];
135+
ans = ans.max(s);
106136
}
107-
f64::from(max) / f64::from(k as i32)
137+
f64::from(ans) / f64::from(k as i32)
108138
}
109139
}
110140
```
@@ -117,16 +147,16 @@ class Solution {
117147
* @return Float
118148
*/
119149
function findMaxAverage($nums, $k) {
120-
$sum = 0;
150+
$s = 0;
121151
for ($i = 0; $i < $k; $i++) {
122-
$sum += $nums[$i];
152+
$s += $nums[$i];
123153
}
124-
$max = $sum;
154+
$ans = $s;
125155
for ($j = $k; $j < count($nums); $j++) {
126-
$sum = $sum - $nums[$j - $k] + $nums[$j];
127-
$max = max($max, $sum);
156+
$s = $s - $nums[$j - $k] + $nums[$j];
157+
$ans = max($ans, $s);
128158
}
129-
return $max / $k;
159+
return $ans / $k;
130160
}
131161
}
132162
```

solution/0600-0699/0643.Maximum Average Subarray I/README_EN.md

+54-24
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@
3737

3838
## Solutions
3939

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)$.
4145

4246
<!-- tabs:start -->
4347

4448
```python
4549
class Solution:
4650
def findMaxAverage(self, nums: List[int], k: int) -> float:
47-
s = sum(nums[:k])
48-
ans = s
51+
ans = s = sum(nums[:k])
4952
for i in range(k, len(nums)):
5053
s += nums[i] - nums[i - k]
5154
ans = max(ans, s)
@@ -69,19 +72,46 @@ class Solution {
6972
}
7073
```
7174

75+
```cpp
76+
class Solution {
77+
public:
78+
double findMaxAverage(vector<int>& nums, int k) {
79+
int s = accumulate(nums.begin(), nums.begin() + k, 0);
80+
int ans = s;
81+
for (int i = k; i < nums.size(); ++i) {
82+
s += nums[i] - nums[i - k];
83+
ans = max(ans, s);
84+
}
85+
return static_cast<double>(ans) / k;
86+
}
87+
};
88+
```
89+
90+
```go
91+
func findMaxAverage(nums []int, k int) float64 {
92+
s := 0
93+
for _, x := range nums[:k] {
94+
s += x
95+
}
96+
ans := s
97+
for i := k; i < len(nums); i++ {
98+
s += nums[i] - nums[i-k]
99+
ans = max(ans, s)
100+
}
101+
return float64(ans) / float64(k)
102+
}
103+
```
104+
72105
```ts
73106
function findMaxAverage(nums: number[], k: number): number {
74-
let n = nums.length;
75-
let ans = 0;
76-
let sum = 0;
77-
// 前k
78-
for (let i = 0; i < k; i++) {
79-
sum += nums[i];
107+
let s = 0;
108+
for (let i = 0; i < k; ++i) {
109+
s += nums[i];
80110
}
81-
ans = sum;
82-
for (let i = k; i < n; i++) {
83-
sum += nums[i] - nums[i - k];
84-
ans = Math.max(ans, sum);
111+
let ans = s;
112+
for (let i = k; i < nums.length; ++i) {
113+
s += nums[i] - nums[i - k];
114+
ans = Math.max(ans, s);
85115
}
86116
return ans / k;
87117
}
@@ -92,13 +122,13 @@ impl Solution {
92122
pub fn find_max_average(nums: Vec<i32>, k: i32) -> f64 {
93123
let k = k as usize;
94124
let n = nums.len();
95-
let mut sum = nums.iter().take(k).sum::<i32>();
96-
let mut max = sum;
125+
let mut s = nums.iter().take(k).sum::<i32>();
126+
let mut ans = s;
97127
for i in k..n {
98-
sum += nums[i] - nums[i - k];
99-
max = max.max(sum);
128+
s += nums[i] - nums[i - k];
129+
ans = ans.max(s);
100130
}
101-
f64::from(max) / f64::from(k as i32)
131+
f64::from(ans) / f64::from(k as i32)
102132
}
103133
}
104134
```
@@ -111,16 +141,16 @@ class Solution {
111141
* @return Float
112142
*/
113143
function findMaxAverage($nums, $k) {
114-
$sum = 0;
144+
$s = 0;
115145
for ($i = 0; $i < $k; $i++) {
116-
$sum += $nums[$i];
146+
$s += $nums[$i];
117147
}
118-
$max = $sum;
148+
$ans = $s;
119149
for ($j = $k; $j < count($nums); $j++) {
120-
$sum = $sum - $nums[$j - $k] + $nums[$j];
121-
$max = max($max, $sum);
150+
$s = $s - $nums[$j - $k] + $nums[$j];
151+
$ans = max($ans, $s);
122152
}
123-
return $max / $k;
153+
return $ans / $k;
124154
}
125155
}
126156
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
double findMaxAverage(vector<int>& nums, int k) {
4+
int s = accumulate(nums.begin(), nums.begin() + k, 0);
5+
int ans = s;
6+
for (int i = k; i < nums.size(); ++i) {
7+
s += nums[i] - nums[i - k];
8+
ans = max(ans, s);
9+
}
10+
return static_cast<double>(ans) / k;
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func findMaxAverage(nums []int, k int) float64 {
2+
s := 0
3+
for _, x := range nums[:k] {
4+
s += x
5+
}
6+
ans := s
7+
for i := k; i < len(nums); i++ {
8+
s += nums[i] - nums[i-k]
9+
ans = max(ans, s)
10+
}
11+
return float64(ans) / float64(k)
12+
}

solution/0600-0699/0643.Maximum Average Subarray I/Solution.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ class Solution {
55
* @return Float
66
*/
77
function findMaxAverage($nums, $k) {
8-
$sum = 0;
8+
$s = 0;
99
for ($i = 0; $i < $k; $i++) {
10-
$sum += $nums[$i];
10+
$s += $nums[$i];
1111
}
12-
$max = $sum;
12+
$ans = $s;
1313
for ($j = $k; $j < count($nums); $j++) {
14-
$sum = $sum - $nums[$j - $k] + $nums[$j];
15-
$max = max($max, $sum);
14+
$s = $s - $nums[$j - $k] + $nums[$j];
15+
$ans = max($ans, $s);
1616
}
17-
return $max / $k;
17+
return $ans / $k;
1818
}
1919
}

solution/0600-0699/0643.Maximum Average Subarray I/Solution.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class Solution:
22
def findMaxAverage(self, nums: List[int], k: int) -> float:
3-
s = sum(nums[:k])
4-
ans = s
3+
ans = s = sum(nums[:k])
54
for i in range(k, len(nums)):
65
s += nums[i] - nums[i - k]
76
ans = max(ans, s)

solution/0600-0699/0643.Maximum Average Subarray I/Solution.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ impl Solution {
22
pub fn find_max_average(nums: Vec<i32>, k: i32) -> f64 {
33
let k = k as usize;
44
let n = nums.len();
5-
let mut sum = nums.iter().take(k).sum::<i32>();
6-
let mut max = sum;
5+
let mut s = nums.iter().take(k).sum::<i32>();
6+
let mut ans = s;
77
for i in k..n {
8-
sum += nums[i] - nums[i - k];
9-
max = max.max(sum);
8+
s += nums[i] - nums[i - k];
9+
ans = ans.max(s);
1010
}
11-
f64::from(max) / f64::from(k as i32)
11+
f64::from(ans) / f64::from(k as i32)
1212
}
1313
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
function findMaxAverage(nums: number[], k: number): number {
2-
let n = nums.length;
3-
let ans = 0;
4-
let sum = 0;
5-
// 前k
6-
for (let i = 0; i < k; i++) {
7-
sum += nums[i];
2+
let s = 0;
3+
for (let i = 0; i < k; ++i) {
4+
s += nums[i];
85
}
9-
ans = sum;
10-
for (let i = k; i < n; i++) {
11-
sum += nums[i] - nums[i - k];
12-
ans = Math.max(ans, sum);
6+
let ans = s;
7+
for (let i = k; i < nums.length; ++i) {
8+
s += nums[i] - nums[i - k];
9+
ans = Math.max(ans, s);
1310
}
1411
return ans / k;
1512
}

solution/0600-0699/0644.Maximum Average Subarray II/README_EN.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,41 @@ Note that we do not consider the subarrays of length &lt; 4.
4242

4343
## Solutions
4444

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)$.
4680

4781
<!-- tabs:start -->
4882

0 commit comments

Comments
 (0)