Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d7bc796

Browse files
authored
feat: add ts solution to lc problem: No.399 (doocs#2998)
1 parent 9bdfdc7 commit d7bc796

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

‎solution/0300-0399/0399.Evaluate Division/README.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,53 @@ impl Solution {
342342
}
343343
```
344344

345+
#### TypeScript
346+
347+
```ts
348+
function calcEquation(equations: string[][], values: number[], queries: string[][]): number[] {
349+
const g: Record<string, [string, number][]> = {};
350+
const ans = Array.from({ length: queries.length }, () => -1);
351+
352+
for (let i = 0; i < equations.length; i++) {
353+
const [a, b] = equations[i];
354+
(g[a] ??= []).push([b, values[i]]);
355+
(g[b] ??= []).push([a, 1 / values[i]]);
356+
}
357+
358+
for (let i = 0; i < queries.length; i++) {
359+
const [c, d] = queries[i];
360+
const vis = new Set<string>();
361+
const q: [string, number][] = [[c, 1]];
362+
363+
if (!g[c] || !g[d]) continue;
364+
if (c === d) {
365+
ans[i] = 1;
366+
continue;
367+
}
368+
369+
for (const [current, v] of q) {
370+
if (vis.has(current)) continue;
371+
vis.add(current);
372+
373+
for (const [intermediate, multiplier] of g[current]) {
374+
if (vis.has(intermediate)) continue;
375+
376+
if (intermediate === d) {
377+
ans[i] = v * multiplier;
378+
break;
379+
}
380+
381+
q.push([intermediate, v * multiplier]);
382+
}
383+
384+
if (ans[i] !== -1) break;
385+
}
386+
}
387+
388+
return ans;
389+
}
390+
```
391+
345392
<!-- tabs:end -->
346393

347394
<!-- solution:end -->

‎solution/0300-0399/0399.Evaluate Division/README_EN.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,53 @@ impl Solution {
340340
}
341341
```
342342

343+
#### TypeScript
344+
345+
```ts
346+
function calcEquation(equations: string[][], values: number[], queries: string[][]): number[] {
347+
const g: Record<string, [string, number][]> = {};
348+
const ans = Array.from({ length: queries.length }, () => -1);
349+
350+
for (let i = 0; i < equations.length; i++) {
351+
const [a, b] = equations[i];
352+
(g[a] ??= []).push([b, values[i]]);
353+
(g[b] ??= []).push([a, 1 / values[i]]);
354+
}
355+
356+
for (let i = 0; i < queries.length; i++) {
357+
const [c, d] = queries[i];
358+
const vis = new Set<string>();
359+
const q: [string, number][] = [[c, 1]];
360+
361+
if (!g[c] || !g[d]) continue;
362+
if (c === d) {
363+
ans[i] = 1;
364+
continue;
365+
}
366+
367+
for (const [current, v] of q) {
368+
if (vis.has(current)) continue;
369+
vis.add(current);
370+
371+
for (const [intermediate, multiplier] of g[current]) {
372+
if (vis.has(intermediate)) continue;
373+
374+
if (intermediate === d) {
375+
ans[i] = v * multiplier;
376+
break;
377+
}
378+
379+
q.push([intermediate, v * multiplier]);
380+
}
381+
382+
if (ans[i] !== -1) break;
383+
}
384+
}
385+
386+
return ans;
387+
}
388+
```
389+
343390
<!-- tabs:end -->
344391

345392
<!-- solution:end -->
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function calcEquation(equations: string[][], values: number[], queries: string[][]): number[] {
2+
const g: Record<string, [string, number][]> = {};
3+
const ans = Array.from({ length: queries.length }, () => -1);
4+
5+
for (let i = 0; i < equations.length; i++) {
6+
const [a, b] = equations[i];
7+
(g[a] ??= []).push([b, values[i]]);
8+
(g[b] ??= []).push([a, 1 / values[i]]);
9+
}
10+
11+
for (let i = 0; i < queries.length; i++) {
12+
const [c, d] = queries[i];
13+
const vis = new Set<string>();
14+
const q: [string, number][] = [[c, 1]];
15+
16+
if (!g[c] || !g[d]) continue;
17+
if (c === d) {
18+
ans[i] = 1;
19+
continue;
20+
}
21+
22+
for (const [current, v] of q) {
23+
if (vis.has(current)) continue;
24+
vis.add(current);
25+
26+
for (const [intermediate, multiplier] of g[current]) {
27+
if (vis.has(intermediate)) continue;
28+
29+
if (intermediate === d) {
30+
ans[i] = v * multiplier;
31+
break;
32+
}
33+
34+
q.push([intermediate, v * multiplier]);
35+
}
36+
37+
if (ans[i] !== -1) break;
38+
}
39+
}
40+
41+
return ans;
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /