From aaef82bac825b3c304913634903d0f29a82b6ae1 Mon Sep 17 00:00:00 2001 From: wu xiangkai Date: Fri, 31 Oct 2025 11:06:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=202025-10-31=E6=89=93=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 121.best-time-to-buy-and-sell-stock.java | 22 ++++++++++++ 123.best-time-to-buy-and-sell-stock-iii.java | 37 ++++++++++++++++++++ 152.maximum-product-subarray.java | 32 +++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 121.best-time-to-buy-and-sell-stock.java create mode 100644 123.best-time-to-buy-and-sell-stock-iii.java create mode 100644 152.maximum-product-subarray.java diff --git a/121.best-time-to-buy-and-sell-stock.java b/121.best-time-to-buy-and-sell-stock.java new file mode 100644 index 0000000..975f151 --- /dev/null +++ b/121.best-time-to-buy-and-sell-stock.java @@ -0,0 +1,22 @@ +/* + * @lc app=leetcode id=121 lang=java + * + * [121] Best Time to Buy and Sell Stock + */ + +// @lc code=start +class Solution { + public int maxProfit(int[] prices) { + if(prices == null || prices.length == 0) { + return 0; + } + int min = prices[0], profit = 0; + for(int i=1; i=0; i--) { + maxProfit = Math.max(maxProfit, rmax - prices[i]); + rmax = Math.max(rmax, prices[i]); + rdp[i] = maxProfit; + } + int profit = ldp[ldp.length-1]; + for(int i=0; i 0) { + curMax = Math.max(nums[i], max*nums[i]); + curMin = Math.min(nums[i], min*nums[i]); + max = curMax; + min = curMin; + } else { + curMax = Math.max(min * nums[i], nums[i]); + curMin = Math.min(max*nums[i], nums[i]); + max = curMax; + min = curMin; + } + r = Math.max(max, r); + } + return r; + } +} +// @lc code=end +