Trains the technique from
LeetCode 399Evaluate DivisionThis 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.
A workshop is documenting a gear train. You are given meshes, where meshes[i] = [driver, driven] names two gears that mesh, together with ratios, where ratios[i] is how many turns driver makes for one single turn of driven.
Each entry of queries is a pair [top, bottom] asking how many turns top makes for one single turn of bottom. A gear that meshes with a chain of others turns in step with all of them, so a query can be settled whenever the two gears are linked through any run of meshes, in either direction.
Return one answer per query, in the order the queries are given, each rounded to 5 decimal places. Answer -1.0 when the query cannot be settled: either one of the two gears is absent from meshes, or both are present but no run of meshes links them. A gear named twice in one query turns exactly once per one of its own turns, so the answer is 1.0 when that gear appears in meshes and -1.0 when it does not.
Example 1
The crank makes 3 turns per spool turn and the spool makes 0.5 turns per drum turn, so the crank makes 1.5 turns per drum turn and the drum makes 0.66667 turns per crank turn. The hub is never mentioned in the meshes, so both queries naming it are unsettled.
Example 2
The recorded mesh reads forward, so reading it backward gives the idler 0.25 turns per cam turn.
Example 3
Two separate trains are documented. Gears in different trains are not linked, so that query is unsettled, and z9 is not documented at all.
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 gear_ratios(meshes: list[list[str]], ratios: list[float], queries: list[list[str]]) -> list[float]:public double[] gearRatios(List<List<String>> meshes, double[] ratios, List<List<String>> queries)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.