Skip to content

Commit cd41196

Browse files
authored
feat: add php solution to lc problem: No.0643 (doocs#975)
1 parent 2f7f9bb commit cd41196

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,30 @@ impl Solution {
121121
}
122122
```
123123

124+
### **PHP**
125+
126+
```php
127+
class Solution {
128+
/**
129+
* @param Integer[] $nums
130+
* @param Integer $k
131+
* @return Float
132+
*/
133+
function findMaxAverage($nums, $k) {
134+
$sum = 0;
135+
for ($i = 0; $i < $k; $i++) {
136+
$sum += $nums[$i];
137+
}
138+
$max = $sum;
139+
for ($j = $k; $j < count($nums); $j++) {
140+
$sum = $sum - $nums[$j - $k] + $nums[$j];
141+
$max = max($max, $sum);
142+
}
143+
return $max / $k;
144+
}
145+
}
146+
```
147+
124148
### **...**
125149

126150
```

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ impl Solution {
109109
}
110110
```
111111

112+
### **PHP**
113+
114+
```php
115+
class Solution {
116+
/**
117+
* @param Integer[] $nums
118+
* @param Integer $k
119+
* @return Float
120+
*/
121+
function findMaxAverage($nums, $k) {
122+
$sum = 0;
123+
for ($i = 0; $i < $k; $i++) {
124+
$sum += $nums[$i];
125+
}
126+
$max = $sum;
127+
for ($j = $k; $j < count($nums); $j++) {
128+
$sum = $sum - $nums[$j - $k] + $nums[$j];
129+
$max = max($max, $sum);
130+
}
131+
return $max / $k;
132+
}
133+
}
134+
```
135+
112136
### **...**
113137

114138
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
/**
3+
* @param Integer[] $nums
4+
* @param Integer $k
5+
* @return Float
6+
*/
7+
function findMaxAverage($nums, $k) {
8+
$sum = 0;
9+
for ($i = 0; $i < $k; $i++) {
10+
$sum += $nums[$i];
11+
}
12+
$max = $sum;
13+
for ($j = $k; $j < count($nums); $j++) {
14+
$sum = $sum - $nums[$j - $k] + $nums[$j];
15+
$max = max($max, $sum);
16+
}
17+
return $max / $k;
18+
}
19+
}

0 commit comments

Comments
 (0)