From c3163e0f7cc8baa14e0614c77c014978202ea7f8 Mon Sep 17 00:00:00 2001 From: Dale Seo Date: Sat, 23 Aug 2025 15:10:35 -0400 Subject: [PATCH] best-time-to-buy-and-sell-stock --- best-time-to-buy-and-sell-stock/DaleSeo.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/DaleSeo.rs diff --git a/best-time-to-buy-and-sell-stock/DaleSeo.rs b/best-time-to-buy-and-sell-stock/DaleSeo.rs new file mode 100644 index 000000000..e7d535e3c --- /dev/null +++ b/best-time-to-buy-and-sell-stock/DaleSeo.rs @@ -0,0 +1,17 @@ +// TC: O(n) +// SC: O(1) +impl Solution { + pub fn max_profit(prices: Vec) -> i32 { + let mut min_price = i32::MAX; + let mut max_profit = 0; + for price in prices { + if price < min_price { + min_price = price; + } + if price - min_price > max_profit { + max_profit = price - min_price; + } + } + max_profit + } +}