美文网首页
买卖股票问题

买卖股票问题

作者: 狼无雨雪 | 来源:发表于2019-07-16 19:46 被阅读0次

问题地址:

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/

解决方法:

#best-time-to-buy-and-sell-stock-i
from typing import *
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        maxProfit = 0
        length = len(prices)
        if not prices or length <= 1:
            return maxProfit
        
        minValue = float("inf")
        maxValue = float("-inf")
        result = 0
        for i in range(length-1):
            
            if prices[i] < minValue:
                minValue = prices[i]
            
            if prices[i+1] > prices[i]:
                maxValue = prices[i+1]
                result = max(result, maxValue - minValue)
            
        return result
#best-time-to-buy-and-sell-stock-ii
from typing import *
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        result = 0
        for i, value in enumerate(prices):
            if i != 0 :
                if value > prices[i-1]:
                    result += value - prices[i-1]
            
        return result
#best-time-to-buy-and-sell-stock-iii
from typing import *
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        k = 2
        
        buys = [float("inf") for _ in range(k)]
        sells = [0 for _ in range(k)]
        
        for v in prices:
            for i in range(k):
                if i == 0:
                    buys[i] = min(buys[i], v)
                    sells[i] = max(sells[i], v - buys[i])
                else:
                    buys[i] = min(buys[i], v - sells[i-1])
                    sells[i] = max(sells[i], v - buys[i])
        return sells[-1]
#best-time-to-buy-and-sell-stock-iv
class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        
        if k == 0 or not prices or len(prices) <= 1:
            return 0
        
        if k >= len(prices)//2:
            result = 0
            for i, v in enumerate(prices):
                if i != 0 and v - prices[i-1] > 0:
                    result += v - prices[i-1]
            return result
        
        buys = [float("inf") for _ in range(k)]
        sells = [0 for _ in range(k)]
        
        for v in prices:
            for i in range(k):
                if i == 0:
                    buys[i] = min(buys[i], v)
                    sells[i] = max(sells[i], v - buys[i])
                else:
                    buys[i] = min(buys[i], v - sells[i-1])
                    sells[i] = max(sells[i], v - buys[i])
        return sells[-1]

相关文章

  • 买卖股票问题

    问题地址: https://leetcode.com/problems/best-time-to-buy-and-...

  • 初级算法(Leetcode分布攻克)

    1. 买卖股票问题 : 买卖股票是Leetcode上的一个系列题,数组的动态规划问题,比较经典,这里记录下我的理解...

  • LeetCode 188. 买卖股票的最佳时机 IV(Best

    188. 买卖股票的最佳时机 IV Python3 实现 动态规划 买卖股票的最佳时机 系列问题 通用型 解法 ...

  • 股票买卖实战技巧(精华)

    支撑线和压力线的买卖实战技巧 股票买卖实战技巧(精华) 股票买卖技巧你知道吗 股票买卖技巧怎样把握 股票买卖技巧助...

  • Week 2019-06-30

    买卖股票的最佳时机 I 买卖股票的最佳时机 II 买卖股票的最佳时机 III

  • 4.交易股票和订单类型

    在本节中,我们将讨论买卖股票的实际问题。个人通常通过使用进行实际交易的持牌经纪公司或经纪人买卖股票。历史上,股票经...

  • ETF基金,把它当股票炒就已经是善用了

    投资者又问了,如果将ETF当成股票来买卖,那么为什么不直接买股票?而买卖指数ETF呢? 本文就为大家解决这个问题,...

  • LeetCode-股票问题

    LeetCode-股票问题 121. 买卖股票的最佳时机[https://leetcode-cn.com/prob...

  • LeetCode 股票问题

    121. 买卖股票的最佳时机122. 买卖股票的最佳时机 II123. 买卖股票的最佳时机 III188. 买卖股...

  • leetcode股票买卖问题

    在看fucking algorithm中的股票问题,记录以下get到的点 在定义dp数组时,dp[i][k][0,...

网友评论

      本文标题:买卖股票问题

      本文链接:https://www.haomeiwen.com/subject/iehxlctx.html