<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刷題實(shí)戰(zhàn)188:買(mǎi)賣(mài)股票的最佳時(shí)機(jī) IV

          共 1041字,需瀏覽 3分鐘

           ·

          2021-02-19 14:39

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

          今天和大家聊的問(wèn)題叫做?買(mǎi)賣(mài)股票的最佳時(shí)機(jī) IV,我們先來(lái)看題面:
          https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv

          You are given an integer array prices where prices[i] is the price of a given stock on the ith day.


          Design an algorithm to find the maximum profit. You may complete at most k transactions.


          Notice that you may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

          題意


          給定一個(gè)整數(shù)數(shù)組 prices ,它的第 i 個(gè)元素 prices[i] 是一支給定的股票在第 i 天的價(jià)格。

          設(shè)計(jì)一個(gè)算法來(lái)計(jì)算你所能獲取的最大利潤(rùn)。你最多可以完成 k 筆交易。

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


          示例


          示例 1:

          輸入:k = 2, prices = [2,4,1]
          輸出:2
          解釋?zhuān)涸诘?1 天 (股票價(jià)格 = 2) 的時(shí)候買(mǎi)入,在第 2 天 (股票價(jià)格 = 4) 的時(shí)候賣(mài)出,這筆交易所能獲得利潤(rùn) = 4-2 = 2 。

          示例 2:

          輸入:k = 2, prices = [3,2,6,5,0,3]
          輸出:7
          解釋?zhuān)涸诘?2 天 (股票價(jià)格 = 2) 的時(shí)候買(mǎi)入,在第 3 天 (股票價(jià)格 = 6) 的時(shí)候賣(mài)出, 這筆交易所能獲得利潤(rùn) = 6-2 = 4 。
          隨后,在第 5 天 (股票價(jià)格 = 0) 的時(shí)候買(mǎi)入,在第 6 天 (股票價(jià)格 = 3) 的時(shí)候賣(mài)出, 這筆交易所能獲得利潤(rùn) = 3-0 = 3 。
          ?

          提示:

          0 <= k <= 100
          0 <= prices.length <= 1000
          0 <= prices[i] <= 1000



          解題


          思路:無(wú)非四種狀態(tài) 當(dāng)天買(mǎi)入不買(mǎi)或者賣(mài)出不賣(mài) ,我們得到狀態(tài)轉(zhuǎn)移方程?

          buy[i]=max(buy[i],sell[i-1]-prices)? ? //buy[i]代表第i筆買(mǎi)入自己還剩的錢(qián) 買(mǎi)入則減去當(dāng)天的價(jià)格
          sell[i]=max(sell[i],buy[i]+prices[i])? ?//selle[i]代表第i筆賣(mài)出后自己還剩的錢(qián) 賣(mài)出即加入當(dāng)天的價(jià)格


          class?Solution?{
          ????public?int?quick(int[] prices){
          ????????int?max=0;
          ????????for(int?i=0;i-1;i++){
          ????????????if(prices[i+1]>prices[i])
          ????????????max+=(prices[i+1]-prices[i]);
          ????????}
          ????????return?max;
          ????}
          ????
          ????public?int?maxProfit(int?k, int[] prices) {
          ????????int?len=prices.length;
          ???????
          ??????if(len==1||len==0||prices==null||k==0){
          ??????????return?0;
          ??????}
          ??????if(k>=len/2){
          ????????return?quick(prices);
          ??????}
          ????????int?[]buy=new?int[k+1];
          ????????int?[]sell=new?int[k+1];
          ????????for(int?i=0;i<=k;i++){
          ????????????buy[i]=Integer.MIN_VALUE;
          ????????}
          ????????for(int?i=0;i????????????for?(int?j = 0; j????????????????buy[j+1] = Math.max(buy[j+1], sell[j] - prices[i]);
          ????????????????sell[j+1] = Math.max(buy[j+1] + prices[i], sell[j+1]);
          ????????????}
          ????????????
          ????????}
          ????????return?sell[k];
          ????????
          ????}
          }


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

          上期推文:

          LeetCode1-180題匯總,希望對(duì)你有點(diǎn)幫助!
          LeetCode刷題實(shí)戰(zhàn)181:超過(guò)經(jīng)理收入的員工
          LeetCode刷題實(shí)戰(zhàn)182:查找重復(fù)的電子郵箱
          LeetCode刷題實(shí)戰(zhàn)183:從不訂購(gòu)的客戶(hù)
          LeetCode刷題實(shí)戰(zhàn)184:部門(mén)工資最高的員工
          LeetCode刷題實(shí)戰(zhàn)185:部門(mén)工資前三高的所有員工
          LeetCode刷題實(shí)戰(zhàn)186:翻轉(zhuǎn)字符串里的單詞 II
          LeetCode刷題實(shí)戰(zhàn)187:重復(fù)的DNA序列

          瀏覽 43
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  涩小说校园春色图片区视频区小说区 | 色婷婷青青草 | 人人操在线 | 五月亚洲色 | 国产美女操逼网 |