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 I - DFS Approach INPUT Conversion Graph 0 Base Unit 1 2 x10 x5 Input Values n = 3 conversions = [ [0, 1, 10], [1, 2, 5] ] ALGORITHM STEPS 1 Build Adjacency List Store edges with factors 2 Initialize result[0] = 1 Base unit equals itself 3 DFS from node 0 Traverse and multiply factors 4 Apply modulo 10^9+7 Prevent overflow DFS Trace Visit 0: result[0] = 1 DFS to 1: 1 * 10 = 10 result[1] = 10 DFS to 2: 10 * 5 = 50 result[2] = 50 FINAL RESULT Conversion Factors from Unit 0 Unit 0 (Base) 1 Unit 1 10 Unit 2 50 Output Array [1, 10, 50] OK - All units converted! DFS traversal complete Key Insight: DFS traverses the conversion graph starting from the base unit (0). Each edge represents a multiplication factor. By multiplying factors along the path, we accumulate the total conversion: 0 --> 1 (x10) --> 2 (x5) = 50. Time: O(n+e), Space: O(n) for visited array and recursion stack. TutorialsPoint - Unit Conversion I | DFS Approach
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