, , , , . O(1) space , ( O(log n)) (O(n)).
, , - O(n), . , . O(n), , .
, . , , .
To do this, you start the start pointer down to the leftmost node and the end pointer down to the righmost node value. You also save two variables to store the last movement (up or across, initially up) of each pointer, so that you can intelligently choose the next move ( front++and end--in your question).
You can then use the current pointers and recent movements along with the current amount to decide which pointer to move (and how).
source
share