Skip to content

Commit 1eb4bf6

Browse files
committed
Add solution 690
1 parent 0f1bb00 commit 1eb4bf6

26 files changed

+859
-655
lines changed

README.md

Lines changed: 473 additions & 472 deletions
Large diffs are not rendered by default.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package leetcode
2+
3+
type Employee struct {
4+
Id int
5+
Importance int
6+
Subordinates []int
7+
}
8+
9+
func getImportance(employees []*Employee, id int) int {
10+
m, queue, res := map[int]*Employee{}, []int{id}, 0
11+
for _, e := range employees {
12+
m[e.Id] = e
13+
}
14+
for len(queue) > 0 {
15+
e := m[queue[0]]
16+
queue = queue[1:]
17+
if e == nil {
18+
continue
19+
}
20+
res += e.Importance
21+
for _, i := range e.Subordinates {
22+
queue = append(queue, i)
23+
}
24+
}
25+
return res
26+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question690 struct {
9+
para690
10+
ans690
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para690 struct {
16+
employees []*Employee
17+
id int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans690 struct {
23+
one int
24+
}
25+
26+
func Test_Problem690(t *testing.T) {
27+
28+
qs := []question690{
29+
30+
{
31+
para690{[]*Employee{{1, 5, []int{2, 3}}, {2, 3, []int{}}, {3, 3, []int{}}}, 1},
32+
ans690{11},
33+
},
34+
}
35+
36+
fmt.Printf("------------------------Leetcode Problem 690------------------------\n")
37+
38+
for _, q := range qs {
39+
_, p := q.ans690, q.para690
40+
fmt.Printf("【input】:%v 【output】:%v\n", p, getImportance(p.employees, p.id))
41+
}
42+
fmt.Printf("\n\n\n")
43+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [690. Employee Importance](https://leetcode.com/problems/employee-importance/)
2+
3+
## 题目
4+
5+
You are given a data structure of employee information, which includes the employee's **unique id**, their **importance value** and their **direct** subordinates' id.
6+
7+
For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is **not direct**.
8+
9+
Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all their subordinates.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
15+
Output: 11
16+
Explanation:
17+
Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
18+
```
19+
20+
**Note:**
21+
22+
1. One employee has at most one **direct** leader and may have several subordinates.
23+
2. The maximum number of employees won't exceed 2000.
24+
25+
## 题目大意
26+
27+
给定一个保存员工信息的数据结构,它包含了员工 唯一的 id ,重要度 和 直系下属的 id 。比如,员工 1 是员工 2 的领导,员工 2 是员工 3 的领导。他们相应的重要度为 15 , 10 , 5 。那么员工 1 的数据结构是 [1, 15, [2]] ,员工 2的 数据结构是 [2, 10, [3]] ,员工 3 的数据结构是 [3, 5, []] 。注意虽然员工 3 也是员工 1 的一个下属,但是由于 并不是直系 下属,因此没有体现在员工 1 的数据结构中。现在输入一个公司的所有员工信息,以及单个员工 id ,返回这个员工和他所有下属的重要度之和。
28+
29+
## 解题思路
30+
31+
- 简单题。根据题意,DFS 或者 BFS 搜索找到所求 id 下属所有员工,累加下属员工的重要度,最后再加上这个员工本身的重要度,即为所求。
32+
33+
## 代码
34+
35+
```go
36+
package leetcode
37+
38+
type Employee struct {
39+
Id int
40+
Importance int
41+
Subordinates []int
42+
}
43+
44+
func getImportance(employees []*Employee, id int) int {
45+
m, queue, res := map[int]*Employee{}, []int{id}, 0
46+
for _, e := range employees {
47+
m[e.Id] = e
48+
}
49+
for len(queue) > 0 {
50+
e := m[queue[0]]
51+
queue = queue[1:]
52+
if e == nil {
53+
continue
54+
}
55+
res += e.Importance
56+
for _, i := range e.Subordinates {
57+
queue = append(queue, i)
58+
}
59+
}
60+
return res
61+
}
62+
```

website/content/ChapterFour/0600~0699/0685.Redundant-Connection-II.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ func findRoot(parent *[]int, k int) int {
113113
----------------------------------------------
114114
<div style="display: flex;justify-content: space-between;align-items: center;">
115115
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0684.Redundant-Connection/">⬅️上一页</a></p>
116-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0693.Binary-Number-with-Alternating-Bits/">下一页➡️</a></p>
116+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0690.Employee-Importance/">下一页➡️</a></p>
117117
</div>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [690. Employee Importance](https://leetcode.com/problems/employee-importance/)
2+
3+
## 题目
4+
5+
You are given a data structure of employee information, which includes the employee's **unique id**, their **importance value** and their **direct** subordinates' id.
6+
7+
For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is **not direct**.
8+
9+
Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all their subordinates.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
15+
Output: 11
16+
Explanation:
17+
Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
18+
```
19+
20+
**Note:**
21+
22+
1. One employee has at most one **direct** leader and may have several subordinates.
23+
2. The maximum number of employees won't exceed 2000.
24+
25+
## 题目大意
26+
27+
给定一个保存员工信息的数据结构,它包含了员工 唯一的 id ,重要度 和 直系下属的 id 。比如,员工 1 是员工 2 的领导,员工 2 是员工 3 的领导。他们相应的重要度为 15 , 10 , 5 。那么员工 1 的数据结构是 [1, 15, [2]] ,员工 2的 数据结构是 [2, 10, [3]] ,员工 3 的数据结构是 [3, 5, []] 。注意虽然员工 3 也是员工 1 的一个下属,但是由于 并不是直系 下属,因此没有体现在员工 1 的数据结构中。现在输入一个公司的所有员工信息,以及单个员工 id ,返回这个员工和他所有下属的重要度之和。
28+
29+
## 解题思路
30+
31+
- 简单题。根据题意,DFS 或者 BFS 搜索找到所求 id 下属所有员工,累加下属员工的重要度,最后再加上这个员工本身的重要度,即为所求。
32+
33+
## 代码
34+
35+
```go
36+
package leetcode
37+
38+
type Employee struct {
39+
Id int
40+
Importance int
41+
Subordinates []int
42+
}
43+
44+
func getImportance(employees []*Employee, id int) int {
45+
m, queue, res := map[int]*Employee{}, []int{id}, 0
46+
for _, e := range employees {
47+
m[e.Id] = e
48+
}
49+
for len(queue) > 0 {
50+
e := m[queue[0]]
51+
queue = queue[1:]
52+
if e == nil {
53+
continue
54+
}
55+
res += e.Importance
56+
for _, i := range e.Subordinates {
57+
queue = append(queue, i)
58+
}
59+
}
60+
return res
61+
}
62+
```
63+
64+
65+
----------------------------------------------
66+
<div style="display: flex;justify-content: space-between;align-items: center;">
67+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0685.Redundant-Connection-II/">⬅️上一页</a></p>
68+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0693.Binary-Number-with-Alternating-Bits/">下一页➡️</a></p>
69+
</div>

website/content/ChapterFour/0600~0699/0693.Binary-Number-with-Alternating-Bits.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ func hasAlternatingBits1(n int) bool {
8282

8383
----------------------------------------------
8484
<div style="display: flex;justify-content: space-between;align-items: center;">
85-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0685.Redundant-Connection-II/">⬅️上一页</a></p>
85+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0690.Employee-Importance/">⬅️上一页</a></p>
8686
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0695.Max-Area-of-Island/">下一页➡️</a></p>
8787
</div>

0 commit comments

Comments
 (0)