|
| 1 | +3528\. Unit Conversion I |
| 2 | + |
| 3 | +Medium |
| 4 | + |
| 5 | +There are `n` types of units indexed from `0` to `n - 1`. You are given a 2D integer array `conversions` of length `n - 1`, where <code>conversions[i] = [sourceUnit<sub>i</sub>, targetUnit<sub>i</sub>, conversionFactor<sub>i</sub>]</code>. This indicates that a single unit of type <code>sourceUnit<sub>i</sub></code> is equivalent to <code>conversionFactor<sub>i</sub></code> units of type <code>targetUnit<sub>i</sub></code>. |
| 6 | + |
| 7 | +Return an array `baseUnitConversion` of length `n`, where `baseUnitConversion[i]` is the number of units of type `i` equivalent to a single unit of type 0. Since the answer may be large, return each `baseUnitConversion[i]` **modulo** <code>10<sup>9</sup> + 7</code>. |
| 8 | + |
| 9 | +**Example 1:** |
| 10 | + |
| 11 | +**Input:** conversions = [[0,1,2],[1,2,3]] |
| 12 | + |
| 13 | +**Output:** [1,2,6] |
| 14 | + |
| 15 | +**Explanation:** |
| 16 | + |
| 17 | +* Convert a single unit of type 0 into 2 units of type 1 using `conversions[0]`. |
| 18 | +* Convert a single unit of type 0 into 6 units of type 2 using `conversions[0]`, then `conversions[1]`. |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +**Example 2:** |
| 23 | + |
| 24 | +**Input:** conversions = [[0,1,2],[0,2,3],[1,3,4],[1,4,5],[2,5,2],[4,6,3],[5,7,4]] |
| 25 | + |
| 26 | +**Output:** [1,2,3,8,10,6,30,24] |
| 27 | + |
| 28 | +**Explanation:** |
| 29 | + |
| 30 | +* Convert a single unit of type 0 into 2 units of type 1 using `conversions[0]`. |
| 31 | +* Convert a single unit of type 0 into 3 units of type 2 using `conversions[1]`. |
| 32 | +* Convert a single unit of type 0 into 8 units of type 3 using `conversions[0]`, then `conversions[2]`. |
| 33 | +* Convert a single unit of type 0 into 10 units of type 4 using `conversions[0]`, then `conversions[3]`. |
| 34 | +* Convert a single unit of type 0 into 6 units of type 5 using `conversions[1]`, then `conversions[4]`. |
| 35 | +* Convert a single unit of type 0 into 30 units of type 6 using `conversions[0]`, `conversions[3]`, then `conversions[5]`. |
| 36 | +* Convert a single unit of type 0 into 24 units of type 7 using `conversions[1]`, `conversions[4]`, then `conversions[6]`. |
| 37 | + |
| 38 | +**Constraints:** |
| 39 | + |
| 40 | +* <code>2 <= n <= 10<sup>5</sup></code> |
| 41 | +* `conversions.length == n - 1` |
| 42 | +* <code>0 <= sourceUnit<sub>i</sub>, targetUnit<sub>i</sub> < n</code> |
| 43 | +* <code>1 <= conversionFactor<sub>i</sub> <= 10<sup>9</sup></code> |
| 44 | +* It is guaranteed that unit 0 can be converted into any other unit through a **unique** combination of conversions without using any conversions in the opposite direction. |
0 commit comments