diff --git a/solution/0100-0199/0121.Best Time to Buy and Sell Stock/README.md b/solution/0100-0199/0121.Best Time to Buy and Sell Stock/README.md index 7ffe5b4fedb04..b4ea8613be039 100644 --- a/solution/0100-0199/0121.Best Time to Buy and Sell Stock/README.md +++ b/solution/0100-0199/0121.Best Time to Buy and Sell Stock/README.md @@ -189,6 +189,27 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $prices + * @return Integer + */ + function maxProfit($prices) { + $win = 0; + $minPrice = $prices[0]; + $len = count($prices); + for ($i = 1; $i < $len; $i++) { + $minPrice = min($minPrice, $prices[$i]); + $win = max($win, $prices[$i] - $minPrice); + } + return $win; + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0121.Best Time to Buy and Sell Stock/README_EN.md b/solution/0100-0199/0121.Best Time to Buy and Sell Stock/README_EN.md index 83abc35b5b499..ec0a4dee24c99 100644 --- a/solution/0100-0199/0121.Best Time to Buy and Sell Stock/README_EN.md +++ b/solution/0100-0199/0121.Best Time to Buy and Sell Stock/README_EN.md @@ -173,6 +173,27 @@ impl Solution { } ``` +### **PHP** + +```php +class Solution { + /** + * @param Integer[] $prices + * @return Integer + */ + function maxProfit($prices) { + $win = 0; + $minPrice = $prices[0]; + $len = count($prices); + for ($i = 1; $i < $len; $i++) { + $minPrice = min($minPrice, $prices[$i]); + $win = max($win, $prices[$i] - $minPrice); + } + return $win; + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.php b/solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.php new file mode 100644 index 0000000000000..9f0d8961724a7 --- /dev/null +++ b/solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.php @@ -0,0 +1,16 @@ +class Solution { + /** + * @param Integer[] $prices + * @return Integer + */ + function maxProfit($prices) { + $win = 0; + $minPrice = $prices[0]; + $len = count($prices); + for ($i = 1; $i < $len; $i++) { + $minPrice = min($minPrice, $prices[$i]); + $win = max($win, $prices[$i] - $minPrice); + } + return $win; + } +} \ No newline at end of file