-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path714.php
More file actions
34 lines (28 loc) · 705 Bytes
/
Copy path714.php
File metadata and controls
34 lines (28 loc) · 705 Bytes
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
<?php
class Solution {
/**
* @param Integer[] $prices
* @param Integer $fee
* @return Integer
*/
function maxProfit($prices, $fee)
{
if (count($prices) <= 1) return 0;
$min = $prices[0];
$profit = 0;
for ($i = 1; $i < count($prices); $i ++) {
if ($prices[$i] < $min) {
$min = $prices[$i];
}
if ($prices[$i] - $min > $fee) {
$profit += ($prices[$i] - $min - $fee);
$min = $prices[$i] - $fee;
}
}
return $profit;
}
}
$prices = [1, 3, 2, 8, 4, 9];
$fee = 2;
$sol = new Solution();
var_dump($sol->maxProfit($prices, $fee));