Skip to content

Commit c68ad0e

Browse files
authored
feat: add php solution to lc problem: No.0121 (doocs#922)
No.0121.Best Time to Buy and Sell Stock
1 parent b4b44cd commit c68ad0e

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

solution/0100-0199/0121.Best Time to Buy and Sell Stock/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,27 @@ impl Solution {
189189
}
190190
```
191191

192+
### **PHP**
193+
194+
```php
195+
class Solution {
196+
/**
197+
* @param Integer[] $prices
198+
* @return Integer
199+
*/
200+
function maxProfit($prices) {
201+
$win = 0;
202+
$minPrice = $prices[0];
203+
$len = count($prices);
204+
for ($i = 1; $i < $len; $i++) {
205+
$minPrice = min($minPrice, $prices[$i]);
206+
$win = max($win, $prices[$i] - $minPrice);
207+
}
208+
return $win;
209+
}
210+
}
211+
```
212+
192213
### **...**
193214

194215
```

solution/0100-0199/0121.Best Time to Buy and Sell Stock/README_EN.md

+21
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ impl Solution {
173173
}
174174
```
175175

176+
### **PHP**
177+
178+
```php
179+
class Solution {
180+
/**
181+
* @param Integer[] $prices
182+
* @return Integer
183+
*/
184+
function maxProfit($prices) {
185+
$win = 0;
186+
$minPrice = $prices[0];
187+
$len = count($prices);
188+
for ($i = 1; $i < $len; $i++) {
189+
$minPrice = min($minPrice, $prices[$i]);
190+
$win = max($win, $prices[$i] - $minPrice);
191+
}
192+
return $win;
193+
}
194+
}
195+
```
196+
176197
### **...**
177198

178199
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
/**
3+
* @param Integer[] $prices
4+
* @return Integer
5+
*/
6+
function maxProfit($prices) {
7+
$win = 0;
8+
$minPrice = $prices[0];
9+
$len = count($prices);
10+
for ($i = 1; $i < $len; $i++) {
11+
$minPrice = min($minPrice, $prices[$i]);
12+
$win = max($win, $prices[$i] - $minPrice);
13+
}
14+
return $win;
15+
}
16+
}

0 commit comments

Comments
 (0)