3528. Unit Conversion I LeetCode Solution

In this guide, you will get 3528. Unit Conversion I LeetCode Solution with the best time and space complexity. The solution to Unit Conversion I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Unit Conversion I solution in C++
  4. Unit Conversion I solution in Java
  5. Unit Conversion I solution in Python
  6. Additional Resources
3528. Unit Conversion I LeetCode Solution image

Problem Statement of Unit Conversion I

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 conversions[i] = [sourceUniti, targetUniti, conversionFactori]. This indicates that a single unit of type sourceUniti is equivalent to conversionFactori units of type targetUniti.
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 109 + 7.

Example 1:

Input: conversions = [[0,1,2],[1,2,3]]
Output: [1,2,6]
Explanation:

Convert a single unit of type 0 into 2 units of type 1 using conversions[0].
Convert a single unit of type 0 into 6 units of type 2 using conversions[0], then conversions[1].

Example 2:

Input: conversions = [[0,1,2],[0,2,3],[1,3,4],[1,4,5],[2,5,2],[4,6,3],[5,7,4]]
Output: [1,2,3,8,10,6,30,24]
Explanation:

Convert a single unit of type 0 into 2 units of type 1 using conversions[0].
Convert a single unit of type 0 into 3 units of type 2 using conversions[1].
Convert a single unit of type 0 into 8 units of type 3 using conversions[0], then conversions[2].
Convert a single unit of type 0 into 10 units of type 4 using conversions[0], then conversions[3].
Convert a single unit of type 0 into 6 units of type 5 using conversions[1], then conversions[4].
Convert a single unit of type 0 into 30 units of type 6 using conversions[0], conversions[3], then conversions[5].
Convert a single unit of type 0 into 24 units of type 7 using conversions[1], conversions[4], then conversions[6].

Constraints:

2 <= n <= 105
conversions.length == n – 1
0 <= sourceUniti, targetUniti < n
1 <= conversionFactori <= 109
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.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3528. Unit Conversion I LeetCode Solution in C++

class Solution {
 public:
  vector<int> baseUnitConversions(vector<vector<int>>& conversions) {
    constexpr int kMod = 1'000'000'007;
    const int n = conversions.size() + 1;
    vector<int> ans(n);
    ans[0] = 1;
    queue<int> q{{0}};
    vector<vector<pair<int, int>>> graph(n);

    for (const vector<int>& conversion : conversions) {
      const int u = conversion[0];
      const int v = conversion[1];
      const int factor = conversion[2];
      graph[u].emplace_back(v, factor);
    }

    while (!q.empty()) {
      const int u = q.front();
      q.pop();
      for (const auto& [v, factor] : graph[u]) {
        ans[v] = (static_cast<long>(ans[u]) * factor) % kMod;
        q.push(v);
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3528. Unit Conversion I LeetCode Solution in Java

class Solution {
  public int[] baseUnitConversions(int[][] conversions) {
    final int MOD = 1_000_000_007;
    final int n = conversions.length + 1;
    int[] ans = new int[n];
    ans[0] = 1;
    Queue<Integer> q = new ArrayDeque<>(Arrays.asList(0));
    List<Pair<Integer, Integer>>[] graph = new List[n];

    for (int i = 0; i < n; i++)
      graph[i] = new ArrayList<>();

    for (int[] conversion : conversions) {
      final int u = conversion[0];
      final int v = conversion[1];
      final int factor = conversion[2];
      graph[u].add(new Pair<>(v, factor));
    }

    while (!q.isEmpty()) {
      final int u = q.poll();
      for (Pair<Integer, Integer> pair : graph[u]) {
        final int v = pair.getKey();
        final int factor = pair.getValue();
        ans[v] = (int) ((long) ans[u] * factor % MOD);
        q.offer(v);
      }
    }

    return ans;
  }
}
// code provided by PROGIEZ

3528. Unit Conversion I LeetCode Solution in Python

class Solution:
  def baseUnitConversions(self, conversions: list[list[int]]) -> list[int]:
    MOD = 1_000_000_007
    n = len(conversions) + 1
    ans = [0] * n
    ans[0] = 1
    q = collections.deque([0])
    graph = [[] for _ in range(n)]

    for u, v, factor in conversions:
      graph[u].append((v, factor))

    while q:
      u = q.popleft()
      for v, factor in graph[u]:
        ans[v] = (ans[u] * factor) % MOD
        q.append(v)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.