【问题标题】:Maximum profit for a share after K transactions. How to return the transactions that actually lead to the max profit?K 笔交易后每股的最大利润。如何返回实际导致最大利润的交易?
【发布时间】:2022-12-03 15:51:17
【问题描述】:

我有这个代码,目前返回一个大小为 n 天的列表的最大利润,有 k 笔交易。 前任。输入:价格 = [1, 5, 2, 3, 7, 6, 4, 5], k = 3,
输出 = 10

但我想将实际的 k 笔交易作为导致最大利润的买卖数组返回

for example ->[(1, 5), (2, 7),(4, 5)], instead of simply 10
#buy at 1, sell at 5...etc

我当前的代码是...

def findMaxProfit(价格,k):

# get the number of days `n`
n = len(price)

# base case
if n <= 1:
    return 0

# profit[i][j] stores the maximum profit gained by doing
# at most `i` transactions till j'th day
profit = [[0 for x in range(n)] for y in range(k + 1)]

# fill profit[][] in a bottom-up fashion
for i in range(k + 1):
    for j in range(n):
        # profit is 0 when
        # i = 0, i.e., for 0th day
        # j = 0, i.e., no transaction is being performed

        if i == 0 or j == 0:
            profit[i][j] = 0
        else:
            max_so_far = 0
            for x in range(j):
                curr_price = price[j] - price[x] + profit[i-1][x]
                if max_so_far < curr_price:
                    max_so_far = curr_price
                    
            profit[i][j] = max(profit[i][j-1], max_so_far)
            
return profit[k][n-1]

我尝试将返回变量更改为 profit[k] ,它返回最大利润的排列 例如: 输入:价格 = [1, 5, 2, 3, 7, 6, 4, 5], k = 3

kp = findMaxProfit(price,k) -> 输出:[0, 4, 4, 5, 9, 9, 9, 10]

我可以使用 for 循环将输出的值映射到价格以获得所需的输出,但是,当数组的大小足够大时,它就不再有效了。

alpha = []
for i in range(1,len(kp)):
  if kp[i-1] != kp[i]:
    alpha.append((price[i-1],price[i]))
delta = []
delta.append(alpha[0])
for i in range(1,len(alpha)):
  if delta[-1][1] == alpha[i][0]:
    delta.append((delta.pop(-1)[0],alpha[i][1]))
  else:
    delta.append(alpha[i])

增量:[(1, 5), (2, 7), (4, 5)]

无论价目表和 k 的大小如何,有什么更好的方法来解决这个问题?

【问题讨论】:

    标签: python algorithm cluster-analysis


    【解决方案1】:

    您使用的方法是正确的,但可以改进实现。您可以存储每笔交易的买入和卖出值,而不是存储每一天和每笔交易的利润。

    这是如何实现的示例:

    def findMaxProfit(price, k):
      n = len(price)
    
      # base case
      if n <= 1:
        return 0
    
      # profit[i][j] stores the maximum profit gained by doing
      # at most `i` transactions till j'th day
      profit = [[0 for x in range(n)] for y in range(k + 1)]
    
      # transactions[i][j] stores the buy and sell values for the
      # `i`th transaction on the `j`th day
      transactions = [[(0,0) for x in range(n)] for y in range(k + 1)]
    
      # fill profit[][] and transactions[][] in a bottom-up fashion
      for i in range(k + 1):
        for j in range(n):
          # profit is 0 when
          # i = 0, i.e., for 0th day
          # j = 0, i.e., no transaction is being performed
    
          if i == 0 or j == 0:
            profit[i][j] = 0
            transactions[i][j] = (0,0)
          else:
            max_so_far = 0
            for x in range(j):
              curr_price = price[j] - price[x] + profit[i-1][x]
              if max_so_far < curr_price:
                max_so_far = curr_price
                transactions[i][j] = (price[x], price[j])
    
            profit[i][j] = max(profit[i][j-1], max_so_far)
    
      # create the array of transactions
      transaction_array = []
      for i in range(k):
        transaction_array.append(transactions[i][n-1])
    
      return transaction_array
    

    这应该返回导致最大利润的交易数组,而不管价目表和 k 的大小。

    希望这可以帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2016-08-29
      • 2011-10-28
      • 1970-01-01
      • 2016-02-25
      • 2021-10-11
      相关资源
      最近更新 更多