Skip to content

Commit aeb61d4

Browse files
committed
690
1 parent f139e62 commit aeb61d4

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

playground.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const api = new Promise((reslove, reject) => {
2+
setTimeout(() => { reslove('ok') }, 1000)
3+
})
4+
5+
const arr = new Array(5)
6+
7+
async function fn() {
8+
for (let i = 0; i < 5; i++) {
9+
api.then(res => {
10+
arr[i] = res
11+
})
12+
}
13+
}
14+
15+
fn()
16+
17+
setTimeout(() => {
18+
console.log(arr)
19+
}, 10000)

problems/690.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Employee {
2+
id: number
3+
importance: number
4+
subordinates: number[]
5+
constructor(id: number, importance: number, subordinates: number[]) {
6+
this.id = (id === undefined) ? 0 : id;
7+
this.importance = (importance === undefined) ? 0 : importance;
8+
this.subordinates = (subordinates === undefined) ? [] : subordinates;
9+
}
10+
}
11+
12+
function getImportance(employees: Employee[], id: number): number {
13+
let sum = 0
14+
15+
const map: Map<number, Employee> = new Map()
16+
for (const item of employees) {
17+
map.set(item.id, item)
18+
}
19+
const fn = (id: number) => {
20+
const employee = map.get(id)
21+
sum += employee.importance
22+
for (const item of employee.subordinates) {
23+
fn(item)
24+
}
25+
}
26+
fn(id)
27+
return sum
28+
};
29+
30+
export default function () {
31+
const a = new Employee(1, 5, [2, 3])
32+
console.log(getImportance([
33+
new Employee(1, 5, [2, 3]),
34+
new Employee(2, 3, []),
35+
new Employee(3, 3, [])
36+
], 1))
37+
}

ts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fn from './problems/897'
1+
import fn from './problems/690'
22

33
fn()
44

tsconfig.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext"
4+
}
5+
}

0 commit comments

Comments
 (0)