• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Uriel's Corner

            Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
            posts - 0, comments - 50, trackbacks - 0, articles - 594
            121. Best Time to Buy and Sell Stock (Easy)
            給一列股票價格,問買賣不超過1次的情況下最多賺多少
            122. Best Time to Buy and Sell Stock III (Medium)
            給一列股票價格,問無限次買賣的情況下最多賺多少(但手里同一時刻只能持有一份股票)
            123. Best Time to Buy and Sell Stock III (Hard)
            給一列股票價格,問買賣不超過2次的情況下最多賺多少
            188. Best Time to Buy and Sell Stock IV (Hard)
            給一列股票價格,問買賣不超過k次的情況下最多賺多少
            121, 123和188類似,都是DP思想,remain[j]:已經買入j次之后最多手頭剩下的錢,profit[j]:已經賣出j次之后最多賺了多少,注意賣出前必須要先買入,188注意特判len(prices)=1的情況
            122貪心,預處理i和i+1天之間的價差,有的賺就都加上

             1 #121
             2 #Runtime: 869 ms
             3 #Memory Usage: 22.5 MB
             4 
             5 class Solution(object):
             6     def maxProfit(self, prices):
             7         """
             8         :type prices: List[int]
             9         :rtype: int
            10         """
            11         remain = 0
            12         profit = 0
            13         remain = prices[0]
            14         for i in range(1, len(prices)):               
            15             remain = min(remain, prices[i])
            16             profit = max(profit, prices[i] - remain)
            17         return profit


             1 #122
             2 #Runtime: 48 ms
             3 #Memory Usage: 14.8 MB
             4 
             5 class Solution(object):
             6     def maxProfit(self, prices):
             7         """
             8         :type prices: List[int]
             9         :rtype: int
            10         """
            11         profit = []
            12         for i in range(len(prices) - 1):
            13             profit.append(prices[i + 1] - prices[i])
            14         ans = 0
            15         for i in profit:
            16             if i > 0:
            17                 ans += i
            18         return ans
            19                 


             1 #123
             2 #Runtime: 1075 ms
             3 #Memory Usage: 25 MB
             4 
             5 class Solution(object):
             6     def maxProfit(self, prices):
             7         """
             8         :type prices: List[int]
             9         :rtype: int
            10         """
            11         remain = [0, -sys.maxint, -sys.maxint]
            12         profit = [0, -sys.maxint, -sys.maxint]
            13         
            14         for i in range(0, len(prices)):
            15             for j in range(1, 3):
            16                 remain[j] = max(remain[j], profit[j - 1] - prices[i])
            17                 profit[j] = max(profit[j], remain[j] + prices[i])
            18         return max(profit)


             1 #188
             2 #Runtime: 250 ms
             3 #Memory Usage: 13.3 MB
             4 
             5 class Solution(object):
             6     def maxProfit(self, k, prices):
             7         """
             8         :type k: int
             9         :type prices: List[int]
            10         :rtype: int
            11         """
            12         remain = [-sys.maxint] * (len(prices) + 1)
            13         profit = [-sys.maxint] * (len(prices) + 1)
            14         remain[0] = 0
            15         profit[0] = 0
            16         for i in range(0, len(prices)):
            17             for j in range(1, min(len(prices) + 1, k + 1)):
            18                 remain[j] = max(remain[j], profit[j - 1] - prices[i])
            19                 profit[j] = max(profit[j], remain[j] + prices[i])
            20         return max(profit)










            欧美成人免费观看久久| 久久99久国产麻精品66| 久久99精品国产一区二区三区| 久久亚洲欧美国产精品| 国产精品久久久久AV福利动漫 | 久久久久一本毛久久久| 久久婷婷色综合一区二区| 亚洲综合熟女久久久30p| 国产精品久久久天天影视| 久久久久国色AV免费看图片| 人妻少妇久久中文字幕| 久久久久久青草大香综合精品| 亚洲AV无码1区2区久久| 久久国产精品免费一区| A狠狠久久蜜臀婷色中文网| 久久亚洲精品无码观看不卡| 国产精品久久久久国产A级| 日本五月天婷久久网站| 久久精品成人欧美大片| 久久久久久综合一区中文字幕| 久久久久亚洲精品日久生情| 久久人人爽人人爽人人片AV东京热| 久久精品国产亚洲av麻豆小说| 国产精品久久久久久久app| 久久精品国产99久久丝袜| 九九久久99综合一区二区| 久久久这里只有精品加勒比| 久久国产乱子伦精品免费午夜| 2021久久国自产拍精品| 久久精品午夜一区二区福利 | 久久久久亚洲精品无码网址| 国产精品毛片久久久久久久| 久久精品亚洲精品国产色婷| 亚洲精品乱码久久久久久蜜桃图片 | 久久AAAA片一区二区| 婷婷久久综合九色综合绿巨人 | 国产亚洲婷婷香蕉久久精品| 久久精品亚洲中文字幕无码麻豆| 久久久久国产精品熟女影院| 久久国产亚洲高清观看| 99久久人妻无码精品系列蜜桃|