Skip to content

Commit 7d76370

Browse files
authored
feat: add solutions to lc problem: No.3528 (#4380)
No.3528.Unit Conversion I
1 parent 7fb7d02 commit 7d76370

File tree

7 files changed

+358
-36
lines changed

7 files changed

+358
-36
lines changed

solution/3500-3599/3528.Unit Conversion I/README.md

+129-18
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ tags:
3434
<p><strong>解释:</strong></p>
3535

3636
<ul>
37-
<li>使用 <code>conversions[0]</code>:将一个 0 类型单位转换为 2 个 1 类型单位。</li>
38-
<li>使用&nbsp;<code>conversions[0]</code>&nbsp;和&nbsp;<code>conversions[1]</code>&nbsp;将一个 0 类型单位转换为 6 个 2 类型单位。</li>
37+
<li>使用 <code>conversions[0]</code>:将一个 0 类型单位转换为 2 个 1 类型单位。</li>
38+
<li>使用&nbsp;<code>conversions[0]</code>&nbsp;&nbsp;<code>conversions[1]</code>&nbsp;将一个 0 类型单位转换为 6 个 2 类型单位。</li>
3939
</ul>
4040
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3500-3599/3528.Unit%20Conversion%20I/images/1745660099-FZhVTM-example1.png" style="width: 545px; height: 119px;" /></div>
4141

@@ -49,13 +49,13 @@ tags:
4949
<p><strong>解释:</strong></p>
5050

5151
<ul>
52-
<li>使用 <code>conversions[0]</code>&nbsp;将一个 0 类型单位转换为 2 个 1 类型单位。</li>
53-
<li>使用 <code>conversions[1]</code>&nbsp;将一个 0 类型单位转换为 3 个 2 类型单位。</li>
54-
<li>使用 <code>conversions[0]</code> 和 <code>conversions[2]</code>&nbsp;将一个 0 类型单位转换为 8 个 3 类型单位。</li>
55-
<li>使用 <code>conversions[0]</code> 和 <code>conversions[3]</code>&nbsp;将一个 0 类型单位转换为 10 个 4 类型单位。</li>
56-
<li>使用 <code>conversions[1]</code> 和 <code>conversions[4]</code>&nbsp;将一个 0 类型单位转换为 6 个 5 类型单位。</li>
57-
<li>使用 <code>conversions[0]</code>、<code>conversions[3]</code> 和 <code>conversions[5]</code>&nbsp;将一个 0 类型单位转换为 30 个 6 类型单位。</li>
58-
<li>使用 <code>conversions[1]</code>、<code>conversions[4]</code> 和 <code>conversions[6]</code>&nbsp;将一个 0 类型单位转换为 24 个 7 类型单位。</li>
52+
<li>使用 <code>conversions[0]</code>&nbsp;将一个 0 类型单位转换为 2 个 1 类型单位。</li>
53+
<li>使用 <code>conversions[1]</code>&nbsp;将一个 0 类型单位转换为 3 个 2 类型单位。</li>
54+
<li>使用 <code>conversions[0]</code> 和 <code>conversions[2]</code>&nbsp;将一个 0 类型单位转换为 8 个 3 类型单位。</li>
55+
<li>使用 <code>conversions[0]</code> 和 <code>conversions[3]</code>&nbsp;将一个 0 类型单位转换为 10 个 4 类型单位。</li>
56+
<li>使用 <code>conversions[1]</code> 和 <code>conversions[4]</code>&nbsp;将一个 0 类型单位转换为 6 个 5 类型单位。</li>
57+
<li>使用 <code>conversions[0]</code>、<code>conversions[3]</code> 和 <code>conversions[5]</code>&nbsp;将一个 0 类型单位转换为 30 个 6 类型单位。</li>
58+
<li>使用 <code>conversions[1]</code>、<code>conversions[4]</code> 和 <code>conversions[6]</code>&nbsp;将一个 0 类型单位转换为 24 个 7 类型单位。</li>
5959
</ul>
6060
</div>
6161

@@ -64,11 +64,11 @@ tags:
6464
<p><strong>提示:</strong></p>
6565

6666
<ul>
67-
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
68-
<li><code>conversions.length == n - 1</code></li>
69-
<li><code>0 &lt;= sourceUnit<sub>i</sub>, targetUnit<sub>i</sub> &lt; n</code></li>
70-
<li><code>1 &lt;= conversionFactor<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
71-
<li>保证单位&nbsp;0 可以通过&nbsp;<strong>唯一&nbsp;</strong>的转换路径(不需要反向转换)转换为任何其他单位。</li>
67+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
68+
<li><code>conversions.length == n - 1</code></li>
69+
<li><code>0 &lt;= sourceUnit<sub>i</sub>, targetUnit<sub>i</sub> &lt; n</code></li>
70+
<li><code>1 &lt;= conversionFactor<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
71+
<li>保证单位&nbsp;0 可以通过&nbsp;<strong>唯一&nbsp;</strong>的转换路径(不需要反向转换)转换为任何其他单位。</li>
7272
</ul>
7373

7474
<!-- description:end -->
@@ -77,32 +77,143 @@ tags:
7777

7878
<!-- solution:start -->
7979

80-
### 方法一
80+
### 方法一:DFS
81+
82+
由于题目保证了单位 0 可以通过唯一的转换路径转换为其他单位,因此我们可以使用深度优先搜索(DFS)来遍历所有单位的转换关系。另外,由于 $\textit{conversions}$ 数组的长度为 $n - 1$,表示有 $n - 1$ 条转换关系,因此我们可以将单位转换关系看作一棵树,根节点为单位 0,其他节点为其他单位。
83+
84+
我们可以用一个邻接表 $g$ 来表示单位转换关系,其中 $g[i]$ 表示单位 $i$ 可以转换到的单位和对应的转换因子。
85+
86+
然后,我们从根节点 $0$ 开始进行深度优先搜索,即调函数 $\textit{dfs}(s, \textit{mul})$,其中 $s$ 表示当前单位,$\textit{mul}$ 表示从单位 $0$ 转换到单位 $s$ 的转换因子。初始时 $s = 0$, $\textit{mul} = 1$。在每次递归中,我们将当前单位 $s$ 的转换因子 $\textit{mul}$ 存储到答案数组中,然后遍历当前单位 $s$ 的所有邻接单位 $t$,递归调用 $\textit{dfs}(t, \textit{mul} \times w \mod (10^9 + 7))$,其中 $w$ 为单位 $s$ 转换到单位 $t$ 的转换因子。
87+
88+
最后,我们返回答案数组即可。
89+
90+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为单位的数量。
8191

8292
<!-- tabs:start -->
8393

8494
#### Python3
8595

8696
```python
87-
97+
class Solution:
98+
def baseUnitConversions(self, conversions: List[List[int]]) -> List[int]:
99+
def dfs(s: int, mul: int) -> None:
100+
ans[s] = mul
101+
for t, w in g[s]:
102+
dfs(t, mul * w % mod)
103+
104+
mod = 10**9 + 7
105+
n = len(conversions) + 1
106+
g = [[] for _ in range(n)]
107+
for s, t, w in conversions:
108+
g[s].append((t, w))
109+
ans = [0] * n
110+
dfs(0, 1)
111+
return ans
88112
```
89113

90114
#### Java
91115

92116
```java
93-
117+
class Solution {
118+
private final int mod = (int) 1e9 + 7;
119+
private List<int[]>[] g;
120+
private int[] ans;
121+
private int n;
122+
123+
public int[] baseUnitConversions(int[][] conversions) {
124+
n = conversions.length + 1;
125+
g = new List[n];
126+
Arrays.setAll(g, k -> new ArrayList<>());
127+
ans = new int[n];
128+
for (var e : conversions) {
129+
g[e[0]].add(new int[] {e[1], e[2]});
130+
}
131+
dfs(0, 1);
132+
return ans;
133+
}
134+
135+
private void dfs(int s, long mul) {
136+
ans[s] = (int) mul;
137+
for (var e : g[s]) {
138+
dfs(e[0], mul * e[1] % mod);
139+
}
140+
}
141+
}
94142
```
95143

96144
#### C++
97145

98146
```cpp
99-
147+
class Solution {
148+
public:
149+
vector<int> baseUnitConversions(vector<vector<int>>& conversions) {
150+
const int mod = 1e9 + 7;
151+
int n = conversions.size() + 1;
152+
vector<vector<pair<int, int>>> g(n);
153+
vector<int> ans(n);
154+
for (const auto& e : conversions) {
155+
g[e[0]].push_back({e[1], e[2]});
156+
}
157+
auto dfs = [&](this auto&& dfs, int s, long long mul) -> void {
158+
ans[s] = mul;
159+
for (auto [t, w] : g[s]) {
160+
dfs(t, mul * w % mod);
161+
}
162+
};
163+
dfs(0, 1);
164+
return ans;
165+
}
166+
};
100167
```
101168
102169
#### Go
103170
104171
```go
172+
func baseUnitConversions(conversions [][]int) []int {
173+
const mod = int(1e9 + 7)
174+
n := len(conversions) + 1
175+
176+
g := make([][]struct{ t, w int }, n)
177+
for _, e := range conversions {
178+
s, t, w := e[0], e[1], e[2]
179+
g[s] = append(g[s], struct{ t, w int }{t, w})
180+
}
181+
182+
ans := make([]int, n)
183+
184+
var dfs func(s int, mul int)
185+
dfs = func(s int, mul int) {
186+
ans[s] = mul
187+
for _, e := range g[s] {
188+
dfs(e.t, mul*e.w%mod)
189+
}
190+
}
191+
192+
dfs(0, 1)
193+
return ans
194+
}
195+
```
105196

197+
#### TypeScript
198+
199+
```ts
200+
function baseUnitConversions(conversions: number[][]): number[] {
201+
const mod = BigInt(1e9 + 7);
202+
const n = conversions.length + 1;
203+
const g: { t: number; w: number }[][] = Array.from({ length: n }, () => []);
204+
for (const [s, t, w] of conversions) {
205+
g[s].push({ t, w });
206+
}
207+
const ans: number[] = Array(n).fill(0);
208+
const dfs = (s: number, mul: number) => {
209+
ans[s] = mul;
210+
for (const { t, w } of g[s]) {
211+
dfs(t, Number((BigInt(mul) * BigInt(w)) % mod));
212+
}
213+
};
214+
dfs(0, 1);
215+
return ans;
216+
}
106217
```
107218

108219
<!-- tabs:end -->

solution/3500-3599/3528.Unit Conversion I/README_EN.md

+129-18
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ tags:
3333
<p><strong>Explanation:</strong></p>
3434

3535
<ul>
36-
<li>Convert a single unit of type 0 into 2 units of type 1 using <code>conversions[0]</code>.</li>
37-
<li>Convert a single unit of type 0 into 6 units of type 2 using <code>conversions[0]</code>, then <code>conversions[1]</code>.</li>
36+
<li>Convert a single unit of type 0 into 2 units of type 1 using <code>conversions[0]</code>.</li>
37+
<li>Convert a single unit of type 0 into 6 units of type 2 using <code>conversions[0]</code>, then <code>conversions[1]</code>.</li>
3838
</ul>
3939
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3500-3599/3528.Unit%20Conversion%20I/images/example1.png" style="width: 545px; height: 118px;" /></div>
4040

@@ -48,25 +48,25 @@ tags:
4848
<p><strong>Explanation:</strong></p>
4949

5050
<ul>
51-
<li>Convert a single unit of type 0 into 2 units of type 1 using <code>conversions[0]</code>.</li>
52-
<li>Convert a single unit of type 0 into 3 units of type 2 using <code>conversions[1]</code>.</li>
53-
<li>Convert a single unit of type 0 into 8 units of type 3 using <code>conversions[0]</code>, then <code>conversions[2]</code>.</li>
54-
<li>Convert a single unit of type 0 into 10 units of type 4 using <code>conversions[0]</code>, then <code>conversions[3]</code>.</li>
55-
<li>Convert a single unit of type 0 into 6 units of type 5 using <code>conversions[1]</code>, then <code>conversions[4]</code>.</li>
56-
<li>Convert a single unit of type 0 into 30 units of type 6 using <code>conversions[0]</code>, <code>conversions[3]</code>, then <code>conversions[5]</code>.</li>
57-
<li>Convert a single unit of type 0 into 24 units of type 7 using <code>conversions[1]</code>, <code>conversions[4]</code>, then <code>conversions[6]</code>.</li>
51+
<li>Convert a single unit of type 0 into 2 units of type 1 using <code>conversions[0]</code>.</li>
52+
<li>Convert a single unit of type 0 into 3 units of type 2 using <code>conversions[1]</code>.</li>
53+
<li>Convert a single unit of type 0 into 8 units of type 3 using <code>conversions[0]</code>, then <code>conversions[2]</code>.</li>
54+
<li>Convert a single unit of type 0 into 10 units of type 4 using <code>conversions[0]</code>, then <code>conversions[3]</code>.</li>
55+
<li>Convert a single unit of type 0 into 6 units of type 5 using <code>conversions[1]</code>, then <code>conversions[4]</code>.</li>
56+
<li>Convert a single unit of type 0 into 30 units of type 6 using <code>conversions[0]</code>, <code>conversions[3]</code>, then <code>conversions[5]</code>.</li>
57+
<li>Convert a single unit of type 0 into 24 units of type 7 using <code>conversions[1]</code>, <code>conversions[4]</code>, then <code>conversions[6]</code>.</li>
5858
</ul>
5959
</div>
6060

6161
<p>&nbsp;</p>
6262
<p><strong>Constraints:</strong></p>
6363

6464
<ul>
65-
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
66-
<li><code>conversions.length == n - 1</code></li>
67-
<li><code>0 &lt;= sourceUnit<sub>i</sub>, targetUnit<sub>i</sub> &lt; n</code></li>
68-
<li><code>1 &lt;= conversionFactor<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
69-
<li>It is guaranteed that unit 0 can be converted into any other unit through a <strong>unique</strong> combination of conversions without using any conversions in the opposite direction.</li>
65+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
66+
<li><code>conversions.length == n - 1</code></li>
67+
<li><code>0 &lt;= sourceUnit<sub>i</sub>, targetUnit<sub>i</sub> &lt; n</code></li>
68+
<li><code>1 &lt;= conversionFactor<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
69+
<li>It is guaranteed that unit 0 can be converted into any other unit through a <strong>unique</strong> combination of conversions without using any conversions in the opposite direction.</li>
7070
</ul>
7171

7272
<!-- description:end -->
@@ -75,32 +75,143 @@ tags:
7575

7676
<!-- solution:start -->
7777

78-
### Solution 1
78+
### Solution 1: DFS
79+
80+
Since the problem guarantees that unit 0 can be converted to any other unit through a unique conversion path, we can use Depth-First Search (DFS) to traverse all unit conversion relationships. Additionally, since the length of the $\textit{conversions}$ array is $n - 1$, representing $n - 1$ conversion relationships, we can treat the unit conversion relationships as a tree, where the root node is unit 0, and the other nodes are the other units.
81+
82+
We can use an adjacency list $g$ to represent the unit conversion relationships, where $g[i]$ represents the units that unit $i$ can convert to and the corresponding conversion factors.
83+
84+
Then, we start the DFS from the root node $0$, i.e., call the function $\textit{dfs}(s, \textit{mul})$, where $s$ represents the current unit, and $\textit{mul}$ represents the conversion factor from unit $0$ to unit $s$. Initially, $s = 0$, $\textit{mul} = 1$. In each recursion, we store the conversion factor $\textit{mul}$ of the current unit $s$ into the answer array, then traverse all adjacent units $t$ of the current unit $s$, and recursively call $\textit{dfs}(t, \textit{mul} \times w \mod (10^9 + 7))$, where $w$ is the conversion factor from unit $s$ to unit $t$.
85+
86+
Finally, we return the answer array.
87+
88+
The complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of units.
7989

8090
<!-- tabs:start -->
8191

8292
#### Python3
8393

8494
```python
85-
95+
class Solution:
96+
def baseUnitConversions(self, conversions: List[List[int]]) -> List[int]:
97+
def dfs(s: int, mul: int) -> None:
98+
ans[s] = mul
99+
for t, w in g[s]:
100+
dfs(t, mul * w % mod)
101+
102+
mod = 10**9 + 7
103+
n = len(conversions) + 1
104+
g = [[] for _ in range(n)]
105+
for s, t, w in conversions:
106+
g[s].append((t, w))
107+
ans = [0] * n
108+
dfs(0, 1)
109+
return ans
86110
```
87111

88112
#### Java
89113

90114
```java
91-
115+
class Solution {
116+
private final int mod = (int) 1e9 + 7;
117+
private List<int[]>[] g;
118+
private int[] ans;
119+
private int n;
120+
121+
public int[] baseUnitConversions(int[][] conversions) {
122+
n = conversions.length + 1;
123+
g = new List[n];
124+
Arrays.setAll(g, k -> new ArrayList<>());
125+
ans = new int[n];
126+
for (var e : conversions) {
127+
g[e[0]].add(new int[] {e[1], e[2]});
128+
}
129+
dfs(0, 1);
130+
return ans;
131+
}
132+
133+
private void dfs(int s, long mul) {
134+
ans[s] = (int) mul;
135+
for (var e : g[s]) {
136+
dfs(e[0], mul * e[1] % mod);
137+
}
138+
}
139+
}
92140
```
93141

94142
#### C++
95143

96144
```cpp
97-
145+
class Solution {
146+
public:
147+
vector<int> baseUnitConversions(vector<vector<int>>& conversions) {
148+
const int mod = 1e9 + 7;
149+
int n = conversions.size() + 1;
150+
vector<vector<pair<int, int>>> g(n);
151+
vector<int> ans(n);
152+
for (const auto& e : conversions) {
153+
g[e[0]].push_back({e[1], e[2]});
154+
}
155+
auto dfs = [&](this auto&& dfs, int s, long long mul) -> void {
156+
ans[s] = mul;
157+
for (auto [t, w] : g[s]) {
158+
dfs(t, mul * w % mod);
159+
}
160+
};
161+
dfs(0, 1);
162+
return ans;
163+
}
164+
};
98165
```
99166
100167
#### Go
101168
102169
```go
170+
func baseUnitConversions(conversions [][]int) []int {
171+
const mod = int(1e9 + 7)
172+
n := len(conversions) + 1
173+
174+
g := make([][]struct{ t, w int }, n)
175+
for _, e := range conversions {
176+
s, t, w := e[0], e[1], e[2]
177+
g[s] = append(g[s], struct{ t, w int }{t, w})
178+
}
179+
180+
ans := make([]int, n)
181+
182+
var dfs func(s int, mul int)
183+
dfs = func(s int, mul int) {
184+
ans[s] = mul
185+
for _, e := range g[s] {
186+
dfs(e.t, mul*e.w%mod)
187+
}
188+
}
189+
190+
dfs(0, 1)
191+
return ans
192+
}
193+
```
103194

195+
#### TypeScript
196+
197+
```ts
198+
function baseUnitConversions(conversions: number[][]): number[] {
199+
const mod = BigInt(1e9 + 7);
200+
const n = conversions.length + 1;
201+
const g: { t: number; w: number }[][] = Array.from({ length: n }, () => []);
202+
for (const [s, t, w] of conversions) {
203+
g[s].push({ t, w });
204+
}
205+
const ans: number[] = Array(n).fill(0);
206+
const dfs = (s: number, mul: number) => {
207+
ans[s] = mul;
208+
for (const { t, w } of g[s]) {
209+
dfs(t, Number((BigInt(mul) * BigInt(w)) % mod));
210+
}
211+
};
212+
dfs(0, 1);
213+
return ans;
214+
}
104215
```
105216

106217
<!-- tabs:end -->

0 commit comments

Comments
 (0)