Opening your workspace…
Workspace/Coding room
Opening your workspace…
A giant snail is carrying the village’s lanterns to the midsummer feast. They hang in a ring around its shell. The snail will let you take one unbroken stretch of lanterns before it trundles away, and the feast needs a certain number of each color.
Choose the shortest stretch that gives the feast all the lanterns it needs. You can start at any lantern and move clockwise, wrapping from the last lantern in the list to the first. You may take extra lanterns, but you can take each lantern only once.
Return [start, length], where start is the zero-based index of the first lantern you take. If several shortest stretches work, choose the smallest starting index. If no stretch works, return [-1, 0].
["gold","blue","blue","red"], {"red":1,"gold":1}Output[3,2]Take the red lantern at index 3, then wrap to the gold lantern at index 0.
["red","blue","red","blue","red"], {"red":2,"blue":1}Output[0,3]The stretches starting at indices 0 and 2 both contain two red lanterns and one blue lantern. Three lanterns are needed, so choose the earlier start.
Scan two copies of the ring while limiting a window to one lap.
function solve(colors, needed) { // 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.