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 be87561b79f96..2d217f0be055f 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 @@ -144,13 +144,13 @@ function maxProfit(prices: number[]): number { ```rust impl Solution { pub fn max_profit(prices: Vec) -> i32 { - let mut res = 0; - let mut min = i32::MAX; - for price in prices { - res = res.max(price - min); - min = min.min(price); + let mut ans = 0; + let mut mi = prices[0]; + for &v in &prices { + ans = ans.max(v - mi); + mi = mi.min(v); } - res + ans } } ``` @@ -197,14 +197,13 @@ class Solution { * @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); + $ans = 0; + $mi = $prices[0]; + foreach ($prices as $v) { + $ans = max($ans, $v - $mi); + $mi = min($mi, $v); } - return $win; + return $ans; } } ``` 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 0422ffacb6964..98b8519df4f52 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 @@ -142,13 +142,13 @@ function maxProfit(prices: number[]): number { ```rust impl Solution { pub fn max_profit(prices: Vec) -> i32 { - let mut res = 0; - let mut min = i32::MAX; - for price in prices { - res = res.max(price - min); - min = min.min(price); + let mut ans = 0; + let mut mi = prices[0]; + for &v in &prices { + ans = ans.max(v - mi); + mi = mi.min(v); } - res + ans } } ``` @@ -195,14 +195,13 @@ class Solution { * @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); + $ans = 0; + $mi = $prices[0]; + foreach ($prices as $v) { + $ans = max($ans, $v - $mi); + $mi = min($mi, $v); } - return $win; + return $ans; } } ``` 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 index 4b93ac4847599..f2bc9b1374280 100644 --- 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 @@ -4,13 +4,12 @@ class Solution { * @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); + $ans = 0; + $mi = $prices[0]; + foreach ($prices as $v) { + $ans = max($ans, $v - $mi); + $mi = min($mi, $v); } - return $win; + return $ans; } -} +} \ No newline at end of file diff --git a/solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.rs b/solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.rs index de7df9f161b47..74398bf5793fc 100644 --- a/solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.rs +++ b/solution/0100-0199/0121.Best Time to Buy and Sell Stock/Solution.rs @@ -1,11 +1,11 @@ impl Solution { pub fn max_profit(prices: Vec) -> i32 { - let mut res = 0; - let mut min = i32::MAX; - for price in prices { - res = res.max(price - min); - min = min.min(price); + let mut ans = 0; + let mut mi = prices[0]; + for &v in &prices { + ans = ans.max(v - mi); + mi = mi.min(v); } - res + ans } }