# Not sure if correct, but is very fast...

• Here is the code. The concept is firstly count all possible profitable transaction as much as possible. This will probably higher than k transactions. Then, we merge each two continuous transactions into one step by step. This will reduce the number of transactions. The merged action will guarantee the least lost of profit. Also, it is possible that we drop one transactions. After that, we calculate the total profit.

``````class Solution(object):
def maxProfit(self, k, prices):

if k == 0:
return 0

if len(prices) < 2:
return 0
elif len(prices) == 2:
return max(0, prices[1]- prices[0])

# do as much as possible

for i in range(1, len(prices)):
if prices[i] > prices[i-1]: # sell
sell = prices[i]
elif prices[i] < prices[i-1]: # buy
#previous sell
if sell is not None:
sellq.append(sell)
sell = None

sellq.append(prices[-1])
else:

for i in range(res):
total = 0

idx = 0
if sellq[i] - buyq[i+1] < cur:
idx = i

idx2 = 0
idx2 = i

if cur2 > cur:
sellq.pop(idx)