From d5bf86926bee253ee029765ca7d76ea814826a5f Mon Sep 17 00:00:00 2001
From: rain84 <rainy_sky@mail.ru>
Date: Sun, 2 Jun 2024 21:54:15 +0300
Subject: [PATCH] feat: add ts solution to lc problem: No.399

---
 .../0399.Evaluate Division/README.md          | 47 +++++++++++++++++++
 .../0399.Evaluate Division/README_EN.md       | 47 +++++++++++++++++++
 .../0399.Evaluate Division/Solution.ts        | 42 +++++++++++++++++
 3 files changed, 136 insertions(+)
 create mode 100644 solution/0300-0399/0399.Evaluate Division/Solution.ts

diff --git a/solution/0300-0399/0399.Evaluate Division/README.md b/solution/0300-0399/0399.Evaluate Division/README.md
index a7946e9f7c761..3e62d62c66a28 100644
--- a/solution/0300-0399/0399.Evaluate Division/README.md	
+++ b/solution/0300-0399/0399.Evaluate Division/README.md	
@@ -342,6 +342,53 @@ impl Solution {
 }
 ```
 
+#### TypeScript
+
+```ts
+function calcEquation(equations: string[][], values: number[], queries: string[][]): number[] {
+    const g: Record<string, [string, number][]> = {};
+    const ans = Array.from({ length: queries.length }, () => -1);
+
+    for (let i = 0; i < equations.length; i++) {
+        const [a, b] = equations[i];
+        (g[a] ??= []).push([b, values[i]]);
+        (g[b] ??= []).push([a, 1 / values[i]]);
+    }
+
+    for (let i = 0; i < queries.length; i++) {
+        const [c, d] = queries[i];
+        const vis = new Set<string>();
+        const q: [string, number][] = [[c, 1]];
+
+        if (!g[c] || !g[d]) continue;
+        if (c === d) {
+            ans[i] = 1;
+            continue;
+        }
+
+        for (const [current, v] of q) {
+            if (vis.has(current)) continue;
+            vis.add(current);
+
+            for (const [intermediate, multiplier] of g[current]) {
+                if (vis.has(intermediate)) continue;
+
+                if (intermediate === d) {
+                    ans[i] = v * multiplier;
+                    break;
+                }
+
+                q.push([intermediate, v * multiplier]);
+            }
+
+            if (ans[i] !== -1) break;
+        }
+    }
+
+    return ans;
+}
+```
+
 <!-- tabs:end -->
 
 <!-- solution:end -->
diff --git a/solution/0300-0399/0399.Evaluate Division/README_EN.md b/solution/0300-0399/0399.Evaluate Division/README_EN.md
index 6d4bc666b9d59..c964e3eed0cfe 100644
--- a/solution/0300-0399/0399.Evaluate Division/README_EN.md	
+++ b/solution/0300-0399/0399.Evaluate Division/README_EN.md	
@@ -340,6 +340,53 @@ impl Solution {
 }
 ```
 
+#### TypeScript
+
+```ts
+function calcEquation(equations: string[][], values: number[], queries: string[][]): number[] {
+    const g: Record<string, [string, number][]> = {};
+    const ans = Array.from({ length: queries.length }, () => -1);
+
+    for (let i = 0; i < equations.length; i++) {
+        const [a, b] = equations[i];
+        (g[a] ??= []).push([b, values[i]]);
+        (g[b] ??= []).push([a, 1 / values[i]]);
+    }
+
+    for (let i = 0; i < queries.length; i++) {
+        const [c, d] = queries[i];
+        const vis = new Set<string>();
+        const q: [string, number][] = [[c, 1]];
+
+        if (!g[c] || !g[d]) continue;
+        if (c === d) {
+            ans[i] = 1;
+            continue;
+        }
+
+        for (const [current, v] of q) {
+            if (vis.has(current)) continue;
+            vis.add(current);
+
+            for (const [intermediate, multiplier] of g[current]) {
+                if (vis.has(intermediate)) continue;
+
+                if (intermediate === d) {
+                    ans[i] = v * multiplier;
+                    break;
+                }
+
+                q.push([intermediate, v * multiplier]);
+            }
+
+            if (ans[i] !== -1) break;
+        }
+    }
+
+    return ans;
+}
+```
+
 <!-- tabs:end -->
 
 <!-- solution:end -->
diff --git a/solution/0300-0399/0399.Evaluate Division/Solution.ts b/solution/0300-0399/0399.Evaluate Division/Solution.ts
new file mode 100644
index 0000000000000..1378ef9991ef4
--- /dev/null
+++ b/solution/0300-0399/0399.Evaluate Division/Solution.ts	
@@ -0,0 +1,42 @@
+function calcEquation(equations: string[][], values: number[], queries: string[][]): number[] {
+    const g: Record<string, [string, number][]> = {};
+    const ans = Array.from({ length: queries.length }, () => -1);
+
+    for (let i = 0; i < equations.length; i++) {
+        const [a, b] = equations[i];
+        (g[a] ??= []).push([b, values[i]]);
+        (g[b] ??= []).push([a, 1 / values[i]]);
+    }
+
+    for (let i = 0; i < queries.length; i++) {
+        const [c, d] = queries[i];
+        const vis = new Set<string>();
+        const q: [string, number][] = [[c, 1]];
+
+        if (!g[c] || !g[d]) continue;
+        if (c === d) {
+            ans[i] = 1;
+            continue;
+        }
+
+        for (const [current, v] of q) {
+            if (vis.has(current)) continue;
+            vis.add(current);
+
+            for (const [intermediate, multiplier] of g[current]) {
+                if (vis.has(intermediate)) continue;
+
+                if (intermediate === d) {
+                    ans[i] = v * multiplier;
+                    break;
+                }
+
+                q.push([intermediate, v * multiplier]);
+            }
+
+            if (ans[i] !== -1) break;
+        }
+    }
+
+    return ans;
+}