Skip to content

Commit d3fb003

Browse files
committed
feat: add ts solution to lc problems: No.1305,1376
- No.1305.All Elements in Two Binary Search Trees - No.1376.Time Needed to Inform All Employees
1 parent 284be75 commit d3fb003

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed

solution/1300-1399/1305.All Elements in Two Binary Search Trees/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,60 @@ impl Solution {
310310
}
311311
```
312312

313+
### **TypeScript**
314+
315+
```ts
316+
/**
317+
* Definition for a binary tree node.
318+
* class TreeNode {
319+
* val: number
320+
* left: TreeNode | null
321+
* right: TreeNode | null
322+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
323+
* this.val = (val===undefined ? 0 : val)
324+
* this.left = (left===undefined ? null : left)
325+
* this.right = (right===undefined ? null : right)
326+
* }
327+
* }
328+
*/
329+
330+
function getAllElements(
331+
root1: TreeNode | null,
332+
root2: TreeNode | null,
333+
): number[] {
334+
const res = [];
335+
const stacks = [[], []];
336+
while (
337+
root1 != null ||
338+
stacks[0].length !== 0 ||
339+
root2 != null ||
340+
stacks[1].length !== 0
341+
) {
342+
if (root1 != null) {
343+
stacks[0].push(root1);
344+
root1 = root1.left;
345+
} else if (root2 != null) {
346+
stacks[1].push(root2);
347+
root2 = root2.left;
348+
} else {
349+
if (
350+
(stacks[0][stacks[0].length - 1] ?? { val: Infinity }).val <
351+
(stacks[1][stacks[1].length - 1] ?? { val: Infinity }).val
352+
) {
353+
const { val, right } = stacks[0].pop();
354+
res.push(val);
355+
root1 = right;
356+
} else {
357+
const { val, right } = stacks[1].pop();
358+
res.push(val);
359+
root2 = right;
360+
}
361+
}
362+
}
363+
return res;
364+
}
365+
```
366+
313367
### **...**
314368

315369
```

solution/1300-1399/1305.All Elements in Two Binary Search Trees/README_EN.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,60 @@ impl Solution {
294294
}
295295
```
296296

297+
### **TypeScript**
298+
299+
```ts
300+
/**
301+
* Definition for a binary tree node.
302+
* class TreeNode {
303+
* val: number
304+
* left: TreeNode | null
305+
* right: TreeNode | null
306+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
307+
* this.val = (val===undefined ? 0 : val)
308+
* this.left = (left===undefined ? null : left)
309+
* this.right = (right===undefined ? null : right)
310+
* }
311+
* }
312+
*/
313+
314+
function getAllElements(
315+
root1: TreeNode | null,
316+
root2: TreeNode | null,
317+
): number[] {
318+
const res = [];
319+
const stacks = [[], []];
320+
while (
321+
root1 != null ||
322+
stacks[0].length !== 0 ||
323+
root2 != null ||
324+
stacks[1].length !== 0
325+
) {
326+
if (root1 != null) {
327+
stacks[0].push(root1);
328+
root1 = root1.left;
329+
} else if (root2 != null) {
330+
stacks[1].push(root2);
331+
root2 = root2.left;
332+
} else {
333+
if (
334+
(stacks[0][stacks[0].length - 1] ?? { val: Infinity }).val <
335+
(stacks[1][stacks[1].length - 1] ?? { val: Infinity }).val
336+
) {
337+
const { val, right } = stacks[0].pop();
338+
res.push(val);
339+
root1 = right;
340+
} else {
341+
const { val, right } = stacks[1].pop();
342+
res.push(val);
343+
root2 = right;
344+
}
345+
}
346+
}
347+
return res;
348+
}
349+
```
350+
297351
### **...**
298352

299353
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function getAllElements(
16+
root1: TreeNode | null,
17+
root2: TreeNode | null,
18+
): number[] {
19+
const res = [];
20+
const stacks = [[], []];
21+
while (
22+
root1 != null ||
23+
stacks[0].length !== 0 ||
24+
root2 != null ||
25+
stacks[1].length !== 0
26+
) {
27+
if (root1 != null) {
28+
stacks[0].push(root1);
29+
root1 = root1.left;
30+
} else if (root2 != null) {
31+
stacks[1].push(root2);
32+
root2 = root2.left;
33+
} else {
34+
if (
35+
(stacks[0][stacks[0].length - 1] ?? { val: Infinity }).val <
36+
(stacks[1][stacks[1].length - 1] ?? { val: Infinity }).val
37+
) {
38+
const { val, right } = stacks[0].pop();
39+
res.push(val);
40+
root1 = right;
41+
} else {
42+
const { val, right } = stacks[1].pop();
43+
res.push(val);
44+
root2 = right;
45+
}
46+
}
47+
}
48+
return res;
49+
}

solution/1300-1399/1376.Time Needed to Inform All Employees/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,41 @@ func max(a, b int) int {
163163
}
164164
```
165165

166+
### **TypeScript**
167+
168+
```ts
169+
function numOfMinutes(
170+
n: number,
171+
headID: number,
172+
manager: number[],
173+
informTime: number[],
174+
): number {
175+
if (n === 1) {
176+
return 0;
177+
}
178+
let res = 0;
179+
const time = new Array(n).fill(0);
180+
time[headID] = -1;
181+
const dfs = (i: number) => {
182+
const aim = manager[i];
183+
if (time[aim] === -1) {
184+
return informTime[aim];
185+
}
186+
if (time[aim] === 0) {
187+
time[aim] = dfs(aim);
188+
}
189+
return time[aim] + informTime[aim];
190+
};
191+
for (let i = 0; i < n; i++) {
192+
if (time[i] === 0) {
193+
time[i] = dfs(i);
194+
}
195+
res = Math.max(res, time[i]);
196+
}
197+
return res;
198+
}
199+
```
200+
166201
### **...**
167202

168203
```

solution/1300-1399/1376.Time Needed to Inform All Employees/README_EN.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,41 @@ func max(a, b int) int {
149149
}
150150
```
151151

152+
### **TypeScript**
153+
154+
```ts
155+
function numOfMinutes(
156+
n: number,
157+
headID: number,
158+
manager: number[],
159+
informTime: number[],
160+
): number {
161+
if (n === 1) {
162+
return 0;
163+
}
164+
let res = 0;
165+
const time = new Array(n).fill(0);
166+
time[headID] = -1;
167+
const dfs = (i: number) => {
168+
const aim = manager[i];
169+
if (time[aim] === -1) {
170+
return informTime[aim];
171+
}
172+
if (time[aim] === 0) {
173+
time[aim] = dfs(aim);
174+
}
175+
return time[aim] + informTime[aim];
176+
};
177+
for (let i = 0; i < n; i++) {
178+
if (time[i] === 0) {
179+
time[i] = dfs(i);
180+
}
181+
res = Math.max(res, time[i]);
182+
}
183+
return res;
184+
}
185+
```
186+
152187
### **...**
153188

154189
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function numOfMinutes(
2+
n: number,
3+
headID: number,
4+
manager: number[],
5+
informTime: number[],
6+
): number {
7+
if (n === 1) {
8+
return 0;
9+
}
10+
let res = 0;
11+
const time = new Array(n).fill(0);
12+
time[headID] = -1;
13+
const dfs = (i: number) => {
14+
const aim = manager[i];
15+
if (time[aim] === -1) {
16+
return informTime[aim];
17+
}
18+
if (time[aim] === 0) {
19+
time[aim] = dfs(aim);
20+
}
21+
return time[aim] + informTime[aim];
22+
};
23+
for (let i = 0; i < n; i++) {
24+
if (time[i] === 0) {
25+
time[i] = dfs(i);
26+
}
27+
res = Math.max(res, time[i]);
28+
}
29+
return res;
30+
}

0 commit comments

Comments
 (0)