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 +