Python 5.7 Buy_And_Sell_Stock_Twice Error

#1

I don’t think the explanation for 5.7 is correct. It states “To combine these two, we compute M[i] = F[i] + B[i] (since the second buy must happen after the first sell)” F is [0, 0, 2, 2, 3, 3, 6, 6, 7] and B is [7, 7, 7, 7, 7, 7, 2, 2, 0] so I don’t see how they got M is [7, 7, 7, 9, 9, 10, 5, 8, 6]. M[i] = F[i] + B[i] would yield [7, 7, 9, 9, 10, 10, 8, 8, 7].

I think they meant M[i] = F[i] + B[i+1] but then that leaves the question of where does the extra 7 at the beginning come from?

0 Likes

#2

I think you’re right, it’s the mistake in the book. M[i] = F[i-1] + B[i], being from i = 1.

0 Likes

#3

@NoobGingrich @masintec The explanation is correct. They are trying to accomplish this M[i] = f[i-1] + B[i]. If you check the calculation now you will get the exact result. Let me know if this clarifies.

0 Likes