令:
dp[i, j] = maximum worth obtainable at period i if we currently hold
stock j
假設t[i] = transaction cost at period i
。
我們:
dp[0, A] = A[0] # buy the best at time 0
dp[0, B] = B[0] # you said only buying A and selling B induces a cost,
# you didn't say buying B induces a cost. If it does,
# just subtract t accordingly below
dp[i, A] = max(
dp[i - 1, B] + A[i] - t[i], # sell B, buy A
A[i] # buy A alone
) # we buy A no matter what
dp[i, B] = max(
dp[i - 1, A] + B[i], # sell A, buy B, - t[i] if needed
B[i] # buy B alone
)
答案是:
max{dp[i, A], dp[i, B]}
i
告訴我們到目前爲止你已經嘗試了什麼?這味道像一個家庭作業/面試問題... –
我認爲你的老師正在尋找你說「動態規劃」。 – Adam
你能舉個例子嗎?我並不十分清楚你是否有初始預算,購買股票是否會最初減少你的利潤。 – IVlad