|
| 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