Opening your workspace…
Workspace/Coding room
Opening your workspace…
A line of ghosts waits on a narrow pier over the Styx. Each holds up a coin. The ferryman can pull alongside either end of the pier, but anyone trying to board from the middle would have to push past a ghost, which is terribly rude even by Underworld standards.
The first ghost aboard may pay with any coin. After that, the fare box accepts a coin only if it is worth strictly more than the last coin it swallowed. Each time a ghost boards, their neighbor becomes the new end of the line. The ferryman can switch ends as often as he likes, but every ghost who hasn't boarded stays in place.
How many ghosts can he get aboard before the fare box puts an end to the trip?
[6,1,4,3,2]Output4Take 2, 3, and 4 from the right, then cross to the left for 6. The ghost holding 1 could only board first, and both ends block them at the start.
[2,3,4,7,2]Output4Both ends offer 2. Start on the left and the next three ghosts can pay 3, 4, and 7. Only one of the two ghosts holding 2 can ever board, so four is the most.
Prefer the smaller valid end. If both ends tie, compare the two increasing runs.
function solve(coins) { // Write your solution here. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.