Skip to content

Commit eecde51

Browse files
committed
Add solution #3535
1 parent 7cb4382 commit eecde51

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2715,6 +2715,7 @@
27152715
3506|[Find Time Required to Eliminate Bacterial Strains](./solutions/3506-find-time-required-to-eliminate-bacterial-strains.js)|Hard|
27162716
3511|[Make a Positive Array](./solutions/3511-make-a-positive-array.js)|Medium|
27172717
3520|[Minimum Threshold for Inversion Pairs Count](./solutions/3520-minimum-threshold-for-inversion-pairs-count.js)|Medium|
2718+
3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium|
27182719

27192720
## License
27202721

solutions/3535-unit-conversion-ii.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* 3535. Unit Conversion II
3+
* https://leetcode.com/problems/unit-conversion-ii/
4+
* Difficulty: Medium
5+
*
6+
* There are n types of units indexed from 0 to n - 1.
7+
*
8+
* You are given a 2D integer array conversions of length n - 1, where
9+
* conversions[i] = [sourceUniti, targetUniti, conversionFactori]. This indicates
10+
* that a single unit of type sourceUniti is equivalent to conversionFactori units
11+
* of type targetUniti.
12+
*
13+
* You are also given a 2D integer array queries of length q, where queries[i] = [unitAi, unitBi].
14+
*
15+
* Return an array answer of length q where answer[i] is the number of units of type unitBi
16+
* equivalent to 1 unit of type unitAi, and can be represented as p/q where p and q are coprime.
17+
* Return each answer[i] as pq-1 modulo 109 + 7, where q-1 represents the multiplicative inverse
18+
* of q modulo 109 + 7.
19+
*/
20+
21+
/**
22+
* @param {number[][]} conversions
23+
* @param {number[][]} queries
24+
* @return {number[]}
25+
*/
26+
var queryConversions = function(conversions, queries) {
27+
const n = conversions.length + 1;
28+
const adj = Array.from({ length: n }, () => []);
29+
const convMap = new Map();
30+
for (const [s, t, f] of conversions) {
31+
adj[s].push(t);
32+
adj[t].push(s);
33+
convMap.set(`${s}-${t}`, BigInt(f));
34+
}
35+
const MOD = 1000000007n;
36+
const ratio = new Array(n).fill(0n);
37+
ratio[0] = 1n;
38+
const visited = new Array(n).fill(false);
39+
visited[0] = true;
40+
const queue = [0];
41+
let queueIndex = 0;
42+
while (queueIndex < queue.length) {
43+
const current = queue[queueIndex++];
44+
for (const neighbor of adj[current]) {
45+
if (visited[neighbor]) continue;
46+
visited[neighbor] = true;
47+
queue.push(neighbor);
48+
const forwardKey = `${current}-${neighbor}`;
49+
if (convMap.has(forwardKey)) {
50+
const factor = convMap.get(forwardKey);
51+
ratio[neighbor] = (ratio[current] * factor) % MOD;
52+
} else {
53+
const backwardKey = `${neighbor}-${current}`;
54+
const factor = convMap.get(backwardKey);
55+
const inverseFactor = modInverse(factor, MOD);
56+
ratio[neighbor] = (ratio[current] * inverseFactor) % MOD;
57+
}
58+
}
59+
}
60+
const result = [];
61+
for (const [unitA, unitB] of queries) {
62+
const ratioA = ratio[unitA];
63+
const ratioB = ratio[unitB];
64+
const inverseRatioA = modInverse(ratioA, MOD);
65+
const conversion = (ratioB * inverseRatioA) % MOD;
66+
result.push(Number(conversion));
67+
}
68+
69+
return result;
70+
71+
function modPow(base, exponent, modulus) {
72+
let result = 1n;
73+
base %= modulus;
74+
while (exponent > 0n) {
75+
if (exponent % 2n === 1n) {
76+
result = (result * base) % modulus;
77+
}
78+
base = (base * base) % modulus;
79+
exponent /= 2n;
80+
}
81+
return result;
82+
}
83+
84+
function modInverse(value, modulus) {
85+
return modPow(value, modulus - 2n, modulus);
86+
}
87+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy