【leetcode】123.(Hard)Best Time to Buy and Sell Stock III
解題思路:
DP,只是儲存的空間是O(1)
這個討論區的答案
首先buy1點是第一次買入點,對於當前點數,只有一個點數的時候只能買入,有兩個及以上的點數時可以選擇買入當前點數或者不買入當前點數即還是使用之前的點數。
sell1是第一次售出點,對於 當前點數可以選擇售出,或者不售出
buy2 sell2同理
題目限制只能有2筆交易,如果有3筆、4筆就依次加入buy3、sell3…即可
提交程式碼:
class Solution {
public int maxProfit(int[] prices) {
int buy1 = Integer.MIN_VALUE, sell1 = 0;
int buy2 = Integer.MIN_VALUE, sell2 = 0;
for (int i = 0; i < prices.length; i++) {
buy1 = Math.max(buy1, -1 * prices[i]);
sell1 = Math.max(sell1, prices[i] + buy1);
buy2 = Math.max(buy2, sell1 - prices[i]);
sell2 = Math.max(sell2, prices[i] + buy2);
}
return sell2;
}
}
執行結果:
相關推薦
【leetcode】123.(Hard)Best Time to Buy and Sell Stock III
解題思路: DP,只是儲存的空間是O(1) 這個討論區的答案 首先buy1點是第一次買入點,對於當前點數,只有一個點數的時候只能買入,有兩個及以上的點數時可以選擇買入當前點數或者不買入當前點數即還是使用之前的點數。 sell1是第一次售出點,對於 當前點數可以選擇售出,或者不售出 buy
【leetcode】122.(Easy)Best Time to Buy and Sell Stock II
解題思路: 迭代更新 時間複雜度:O(n) 提交程式碼:滑動視窗 class Solution { public int maxProfit(int[] prices) { if(prices.length==0) return 0;
【leetcode】121.(Easy)Best Time to Buy and Sell Stock
解題思路: 方法一:滑動視窗 時間複雜度:O(n) 方法二:迭代更新 每當有更低價時更新買價, 每當有更高價時更新賣價, 期間更新每一次的買價和賣價的差價 另一方面,為了避免用“今天的低價”購買“昨天的高價”,在有更低價時同時更新買價和賣價(繼而當
【python/Hard/leetcode/123】Best Time to Buy and Sell Stock III
題目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 基本思路 開闢兩個陣列p1和p2 p1[i]表示在price[i]之前進行一次交易所獲得的最大利潤, p2[i]表示在price[i
【LeetCode】123.Best Time to Buy and Sell Stock III
題目描述(Hard) Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the
【LeetCode】#123買賣股票的最佳時機III(Best Time to Buy and Sell Stock III)
【LeetCode】#123買賣股票的最佳時機III(Best Time to Buy and Sell Stock III) 題目描述 給定一個數組,它的第 i 個元素是一支給定的股票在第 i 天的價格。 設計一個演算法來計算你所能獲取的最大利潤。你最多可以完成 兩筆 交易。 注意
leetcode——Best Time to Buy and Sell Stock III 買賣股票最大收益(AC)
element cti () -- 最大 leetcode price imu cto Say you have an array for which the ith element is the price of a given stock on day i. D
<LeetCode OJ> 121. /122. Best Time to Buy and Sell Stock(I / II)
今天 tar article 必須 font details let have 獲得 Say you have an array for which the ith element is the price of a given stock
Best Time to Buy and Sell Stock III 最佳時間買入賣出股票(最多兩次買賣)@LeetCode
轉載:https://blog.csdn.net/fightforyourdream/article/details/14503469 題目: 最佳時間買入賣出股票:你有一個數組儲存了股票在第i天的價錢,現在你最多進行兩次買賣,但同一時間你手上只能保持一個股票,如何賺的最多 思路:
[LeetCode] 123. Best Time to Buy and Sell Stock III 買賣股票的最佳時間 III
AC range ger AR sign self you www release Say you have an array for which the ith element is the price of a given stock on day i. Design
LeetCode 123 :Best Time to Buy and Sell Stock III
該問題和121,122是一類問題,難度逐層遞增,首先先考慮121題 只能交易一次,尋找最大利潤,一開始想到的最簡單的策略就是用一個值儲存目前為止碰到的最小進價,和當前值相減後與當前最大利潤比較,這樣只需要兩個額外變數即可,但
[leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股時機之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You
LeetCode 123. Best Time to Buy and Sell Stock III (stock problem)
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete a
leetcode | 123. Best Time to Buy and Sell Stock III
題目 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You
Leetcode 123. Best Time to Buy and Sell Stock III
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. Y
Leetcode 123 Best Time to Buy and Sell Stock III 至多兩次買賣股票最大收益
題目描述 Say you have an array for which the ith element is the price of a given stock on day i. 假設你有一個數組,裡面記錄的是每一天的股票的價格。 Desig
leetcode 123. Best Time to Buy and Sell Stock III 解題報告
標籤為Hard,動態規劃題目 與一般的股票題目類似,但是最多支援兩次賣出操作,很容易想到多開一個維度。 設F[i][j]為第i次操作後第j天賣出時能得到的最大收益 有F[i][j]=max{F[i-1][0..j-1]}+prices[j]-min{prices
Best Time to Buy and Sell Stock III(JAVA)-動態規劃
題目: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maxim
Best Time to Buy and Sell Stock III 兩次買賣股票的最佳時機 (重重重)
題目: 只能買賣兩次,而且最大手裡只有一隻股票。求最大利潤。 解答: 參考: 程式碼: /* 解釋: 首先,因為能買2次(第一次的賣可以和第二次的買在同一時間),但第二次的買不能在第一次的賣左邊。 所以維護2個表,f1和f2,size都和prices一
#123 Best Time to Buy and Sell Stock III
ret 交易 define find one 之間 sel tor arp /* 找一次操作能獲取最大利潤的一次交易(即題目121)的idxBuy和idxSell,這次交易稱為首次交易 然後分5中情況: 情況1:最優解就只有“首次交易” 情況2:“首次交易”加上左邊的交易