Trains the technique from
LeetCode 1642Furthest Building You Can ReachThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Relay masts stand in a row along a ridge, and masts[i] is the height of mast i in metres. A technician begins at mast 0 and only ever moves to the mast immediately to the right.
Moving from mast i to mast i + 1 is free whenever masts[i + 1] <= masts[i]. When masts[i + 1] > masts[i], that one move has to be paid for in exactly one of two ways:
masts[i + 1] - masts[i] metres out of the cable metres on the reel, orhoists, which covers a rise of any size.Both supplies are shared across the whole walk, and neither is refunded. Spend them however you like, and return the index of the furthest mast the technician can end up standing on.
Example 1
Spending the hoist on the 3 metre rise from mast 0 to mast 1 gets the technician there, and mast 1 to mast 2 drops in height so it is free. The rise from mast 2 to mast 3 is 6 metres, with no hoist left and only 2 metres of cable, so mast 2 is the end of the walk.
Example 2
The first move drops, the second is level, and both are free. Paying the 4 metre rise into mast 3 empties the reel, and the 2 metre rise into mast 4 then cannot be paid at all.
Example 3
The single 7 metre rise is covered by the one hoist, which works for a rise of any size.
Example 4
Neither move goes up, so both are free and the empty supplies never matter.
Example 5
Paying the 3 metre rise into mast 1 with cable and the 18 metre rise into mast 3 with the hoist reaches mast 3. The 1 metre rise into mast 4 is then unpayable, since the reel is empty and the hoist is gone.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def furthest_building(masts: list[int], cable: int, hoists: int) -> int:public int furthestBuilding(int[] masts, int cable, int hoists)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.