I solved the week problem, and it runs in 0.036s.
The problem was pretty easy to understand, and quick to code. Some things were explained not so clearly. I spend the most time debugging and making sure special situations were handled well.
The hardest part was a border case. To handle this border case I had to rewrite my whole algorithm.