Unit Conversion II is an advanced graph traversal problem that simulates a unit conversion system.

You're given n types of units (indexed 0 to n-1) and a 2D array conversions where conversions[i] = [sourceUnit, targetUnit, conversionFactor] means that 1 unit of sourceUnit = conversionFactor units of targetUnit.

For each query [unitA, unitB], you need to find how many units of unitB are equivalent to 1 unit of unitA. The answer should be expressed as a fraction p/q in lowest terms, then return p × q⁻¹ mod (10⁹ + 7) where q⁻¹ is the modular multiplicative inverse of q.

Key Challenge: Handle indirect conversions (A→C via A→B→C) and return mathematically correct fractions modulo 10⁹ + 7.

Input & Output

Constraints

Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen