From 2d66c713f06609bfcbfa6099b4e858686ae620a2 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 13:58:57 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200121.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA.md=20abo?= =?UTF-8?q?ut=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0121.买卖股票的最佳时机.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index 753cb106..be8630f5 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -510,7 +510,22 @@ public class Solution } ``` +Rust: +> 贪心 + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let (mut low, mut res) = (i32::MAX, 0); + for p in prices { + low = p.min(low); + res = res.max(p - low); + } + res + } +} +```

From b716a8ab44c06d9ff0b144774aa43b93efd75c90 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Sat, 3 Jun 2023 14:05:04 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200121.=E4=B9=B0=E5=8D=96=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6=E6=9C=BA.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0121.买卖股票的最佳时机.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index be8630f5..9c8a4390 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -527,6 +527,21 @@ impl Solution { } ``` +> 动态规划 + +```rust +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let mut dp = vec![-prices[0], 0]; + for p in prices { + dp[0] = dp[0].max(-p); + dp[1] = dp[1].max(dp[0] + p); + } + dp[1] + } +} +``` +