# LeetCode 123 – Best Time to Buy and Sell Stock III

#### Posted on January 2, 2014

##### Last updated on January 2, 2014

Solution to LeetCode problem Best Time to Buy and Sell Stock III.

This is quite similar to the Codility Max-Double-Slice Sum problem.

We compute M1[i] – the maximal profit possible considering all the prices up to index $$i$$, and similarly, M2[i] – the max profit considering all the prices from prices[i..N-1]. The simply iterate through every index $$i$$, and find the maximum of M1[i] + M2[i]. This takes all the required cases into consideration.

Here is a version with a little more expansion so that it’s easier to see what’s going on.

Markdown SHA1: 80bd15d33837df9dbb5615c23fe53d3fdc4f0c8a