Unit Conversion I - Problem

Imagine you're building a universal unit converter for a scientific application! You have n different types of units (indexed from 0 to n-1), and you're given conversion relationships between them.

You receive a 2D array conversions where each element conversions[i] = [sourceUnit, targetUnit, conversionFactor] tells you that 1 unit of sourceUnit equals conversionFactor units of targetUnit.

Here's the challenge: Convert everything to a base unit system! You need to find how many units of each type are equivalent to 1 unit of type 0 (our base unit).

Since the conversion factors can get very large, return each result modulo 109 + 7.

Example: If 1 meter = 100 cm, and 1 cm = 10 mm, then 1 meter = 1000 mm. We want to express everything in terms of meters (unit 0).

Input & Output

example_1.py โ€” Basic Unit Conversion
$ Input: n = 3, conversions = [[0,1,10],[1,2,5]]
โ€บ Output: [1, 10, 50]
๐Ÿ’ก Note: Unit 0 is base (factor 1). 1 unit of type 0 = 10 units of type 1, so factor for type 1 is 10. 1 unit of type 1 = 5 units of type 2, so 1 unit of type 0 = 10ร—5 = 50 units of type 2.
example_2.py โ€” Disconnected Units
$ Input: n = 4, conversions = [[0,1,2],[2,3,3]]
โ€บ Output: [1, 2, 0, 0]
๐Ÿ’ก Note: Units 2 and 3 are not connected to unit 0, so their conversion factors are 0. Only units 0 and 1 are in the same connected component.
example_3.py โ€” Large Factors
$ Input: n = 2, conversions = [[0,1,1000000]]
โ€บ Output: [1, 1000000]
๐Ÿ’ก Note: Direct conversion: 1 unit of type 0 equals 1,000,000 units of type 1. Since 1,000,000 < 10^9+7, no modulo needed.

Constraints

  • 1 โ‰ค n โ‰ค 103
  • conversions.length == n - 1 or fewer (tree or forest structure)
  • 0 โ‰ค sourceUniti, targetUniti < n
  • 1 โ‰ค conversionFactori โ‰ค 109
  • All conversion factors are positive integers

Visualization

Tap to expand
Unit Conversion as Graph TraversalConnected Component0Base: 11Factor: 32Factor: 15ร—3ร—5Disconnected Component3Factor: 04Factor: 0ร—2Algorithm Steps1. Build Graph: Create adjacency list with conversion factors as edge weights2. Start DFS: Begin from unit 0 with initial factor = 13. Propagate: For each edge (uโ†’v, weight w): factor[v] = factor[u] ร— w4. Mark Unreachable: Units not visited get factor = 05. Apply Modulo: Return all factors mod (10โน + 7)
Understanding the Visualization
1
Model as Graph
Each unit is a node, conversions are weighted edges
2
Start from Base
Begin DFS/BFS from unit 0 with factor 1
3
Propagate Factors
Multiply factors along edges as we traverse the graph
4
Handle Disconnected
Units not reachable from base get factor 0
Key Takeaway
๐ŸŽฏ Key Insight: Model as weighted graph + single DFS traversal from base unit = O(n) optimal solution!
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.5K Views
Medium Frequency
~18 min Avg. Time
892 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