<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)123:買賣股票的最佳時機 III

          共 1671字,需瀏覽 4分鐘

           ·

          2020-12-16 21:15

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?買賣股票的最佳時機 III,我們先來看題面:
          https://leetcode-cn.com/problems/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 may complete at most two transactions.


          Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

          題意


          給定一個數(shù)組,它的第 i 個元素是一支給定的股票在第 i 天的價格。

          設(shè)計一個算法來計算你所能獲取的最大利潤。你最多可以完成 兩筆 交易。

          注意: 你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。

          樣例


          解題

          https://blog.csdn.net/qq_17550379/article/details/83620892

          有了前面兩個問題的基礎(chǔ),我們首先想通過貪心解決這個問題。但是這個問題不行,因為有最多兩比交易的限制。但是正因為如此這個問題也變得非常容易,我們可以參考第一個問題的解決思路。我們主要有這樣幾種狀態(tài)buy1、buy2、sell1和sell2,涉及的狀態(tài)方程為

          然后就是考慮邊界問題,很顯然buy1[0]=-prices[0],而sell1=0(相當(dāng)于買入后再賣出)、buy2-prices[0](相當(dāng)于買入后再賣出再買入)、sell2=0(相當(dāng)于買入后再賣出再買入再賣出)。最后我們只要只需要返回sell2即可,因為sell2>=sell1一定成立。最后Python代碼如下:

          class?Solution:
          ????def?maxProfit(self, prices):
          ????????"""
          ????????:type prices: List[int]
          ????????:rtype: int
          ????????"""

          ????????if?not?prices:
          ????????????return?0

          ????????len_prices = len(prices)
          ????????buy1, sell1, buy2, sell2 = -prices[0], 0, -prices[0], 0

          ????????for?i in?range(1, len_prices):
          ????????????buy1 = max(buy1, -prices[i])
          ????????????sell1 = max(sell1, buy1 + prices[i])
          ????????????buy2 = max(buy2, sell1 - prices[i])
          ????????????sell2 = max(sell2, buy2 + prices[i])

          ????????return?sell2



          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。

          上期推文:

          LeetCode1-120題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)121:買賣股票的最佳時機


          瀏覽 33
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品秘 入口免费直播大尺度 | 竹菊影视一区二区三区四区 | 大香蕉伊人视频网站 | 中文字幕永久地址 | 台湾无码在线观看 |