2024/8/26 每日一题
This commit is contained in:
parent
12a0861326
commit
e488b9cad9
|
@ -0,0 +1,16 @@
|
||||||
|
package com.cool.one_question_per_day;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/08/26/22:45
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
class Employee {
|
||||||
|
public int id;
|
||||||
|
public int importance;
|
||||||
|
public List<Integer> subordinates;
|
||||||
|
};
|
|
@ -0,0 +1,76 @@
|
||||||
|
package com.cool.one_question_per_day;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created with IntelliJ IDEA.
|
||||||
|
*
|
||||||
|
* @Author: Cool
|
||||||
|
* @Date: 2024/08/26/22:36
|
||||||
|
* DayNumber 1
|
||||||
|
* Hard 2
|
||||||
|
*/
|
||||||
|
public class LeetCode20240826 {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author Cool
|
||||||
|
* @Date 23:05 2024/8/26
|
||||||
|
* @Description 广搜
|
||||||
|
**/
|
||||||
|
// public int getImportance(List<Employee> employees, int id) {
|
||||||
|
// Queue<Integer> queue = new PriorityQueue<>();
|
||||||
|
// int res = 0;
|
||||||
|
// Map<Integer, Employee> map = employees.stream().collect(Collectors.toMap(employee -> employee.id, employee -> employee));
|
||||||
|
// /**
|
||||||
|
// * 这个更快,流处理更装逼
|
||||||
|
// *
|
||||||
|
// **/
|
||||||
|
// // Map<Integer, Employee> map = new HashMap<Integer, Employee>();
|
||||||
|
// // for (Employee employee : employees) {
|
||||||
|
// // map.put(employee.id, employee);
|
||||||
|
// // }
|
||||||
|
// //
|
||||||
|
// queue.addAll(map.get(id).subordinates);
|
||||||
|
// res+=map.get(id).importance;
|
||||||
|
// while (!queue.isEmpty()) {
|
||||||
|
// Integer header = queue.poll();
|
||||||
|
// Employee employee = map.get(header);
|
||||||
|
// queue.addAll(employee.subordinates);
|
||||||
|
// res+=employee.importance;
|
||||||
|
// }
|
||||||
|
// return res;
|
||||||
|
// }
|
||||||
|
Map<Integer, Employee> map = new HashMap<>();
|
||||||
|
|
||||||
|
public int getImportance(List<Employee> employees, int id) {
|
||||||
|
int res = 0;
|
||||||
|
|
||||||
|
for (Employee employee : employees) {
|
||||||
|
map.put(employee.id, employee);
|
||||||
|
}
|
||||||
|
Employee employee = map.get(id);
|
||||||
|
res += employee.importance;
|
||||||
|
for (Integer subordinate : employee.subordinates) {
|
||||||
|
res += dfs(map.get(subordinate));
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author Cool
|
||||||
|
* @Date 23:10 2024/8/26
|
||||||
|
* @Description 递归深搜
|
||||||
|
**/
|
||||||
|
|
||||||
|
private int dfs(Employee employee) {
|
||||||
|
int res = 0;
|
||||||
|
res += employee.importance;
|
||||||
|
for (Integer subordinate : employee.subordinates) {
|
||||||
|
Employee employee1 = map.get(subordinate);
|
||||||
|
res += dfs(employee1);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue