Asked at Squarepoint Capital
Theory: Algorithms Under a Complexity BoundRead the problem, hints and solution here. The editor needs a bigger screen: open this page on a laptop to write and run your code.
Available in Python and C++. You are solving it in Python. Switch to C++
A frog crosses a pond over a row of \(n\) lily pads, numbered 0 to \(n - 1\). Landing on pad \(i\) costs \(c_i\). The frog starts on the near bank, just before pad 0, and has to reach the far bank, just after pad \(n - 1\). Every jump moves it forward by 1 or 2 positions, counting the two banks as positions:
The banks cost nothing. Implement min_crossing_cost(costs) returning the
smallest total cost of the pads the frog lands on.
With no pads the frog is already across, so the answer is 0. With one pad it can jump straight over it, so the answer is 0 as well.
min_crossing_cost([10, 15, 20])
# 15 (bank -> pad 1 -> far bank)
min_crossing_cost([1, 100, 1, 1, 1, 100, 1, 1, 100, 1])
# 6 (pads 0, 2, 4, 6, 7 and 9)
long long min_crossing_cost(const std::vector<long long>& costs);