Opening your workspace…
Workspace/Coding room
Opening your workspace…
A goblin conductor hands you a brass lantern and waves you aboard a tiny train. Your little sister has been waiting all week to see the glowworms in the tunnels. You can board at any station, and you want to take her through as many tunnels as possible in one ride.
The stations are in a straight line, and the train only runs forward. Each tunnel changes the lantern’s charge by a fixed amount. A positive number adds charge, a negative number drains it, and zero leaves it the same. The train can take you through a tunnel only if the lantern will have at least zero charge after crossing.
You start with charge units wherever you board. You can end the ride at any station. What is the greatest number of consecutive tunnels you can cross? Return 0 if you can’t cross any.
changes[i] is the charge change for the tunnel from station i to station i + 1. Stations are numbered from 0.
3, [-4,2,-5,4,-3,1]Output5Board at station 1. The lantern has 5, 0, 4, 1, 2 units after the five crossings. A six-tunnel ride would start at station 0, where the first tunnel costs 4 units.
2, [2,-4,3,-6,2]Output3Board at station 0. After three crossings, the lantern has 3 units, too few for the fourth tunnel. The only other possible start for a four-tunnel ride is station 1, where the first crossing would cost 4 units.
Track the minimum prefix charge in each candidate window with a monotone deque.
function solve(charge, changes) { // 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.