Best time to buy and sell the stock
Idea: Separate the conditional that the i-th day you have the stock or not have the stock
So that,
T[i][k] should be split into two part: T[i][k][0] and T[i][k][1]
T[i][k][0] denotes the maximum profit at the end of i-th day with at most k transactions with 0 stack in our hand After take the action.
T[i][k][1] denotes the maximum profit at the end of i-th day with at most k transactions with 1 stack in our hand After take the action.
T[i][k][1] = max(T[i-1][k][1], T[i-1][k][0] - price[i])
T[i][k][0] = max(T[i-1][k][0], T[i-1][k-1][1] + price[i])