690. Employee Importance(dfs bfs) 1834. Single-Threaded CPU

690. Employee Importance(dfs bfs) 1834. Single-Threaded CPU_第1张图片
我做的是广度遍历,根据第一个员工找子员工放入队列中,再根据子员工找出其下一个员工。。。。

 public int getImportance(List employees, int id) {
        //存储员工id
        Queue queue=new ArrayDeque<>();
        queue.add(id);
        //员工id 价值
        Map map=new HashMap<>();
        //员工id 子员工
        Map> map1=new HashMap<>();
        for (int i = 0; i < employees.size(); i++) {
            map.put(employees.get(i).id,employees.get(i).importance);
            map1.put(employees.get(i).id,employees.get(i).subordinates);
        }
        int res=0;
        while (!queue.isEmpty())
        {
            res+=map.get(queue.peek());
//            增加子员工
            for (int i = 0; i < map1.get(queue.peek()).size(); i++) {
                queue.add(map1.get(queue.peek()).get(i));
            }
            queue.poll();
        }



        return res;
    }

官方给出深度遍历

class Solution {
    Map map = new HashMap();

    public int getImportance(List employees, int id) {
        for (Employee employee : employees) {
            map.put(employee.id, employee);
        }
        return dfs(id);
    }

    public int dfs(int id) {
        Employee employee = map.get(id);
        int total = employee.importance;
        List subordinates = employee.subordinates;
        for (int subId : subordinates) {
            total += dfs(subId);
        }
        return total;
    }
}

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/employee-importance/solution/yuan-gong-de-zhong-yao-xing-by-leetcode-h6xre/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

单线程cpu 短作业优先算法

690. Employee Importance(dfs bfs) 1834. Single-Threaded CPU_第2张图片

class Solution {
 class Task{
    int id;
    int enqueueTime;
    int processingTime;

    public Task(int id, int enqueueTime, int processingTime){
      this.id = id;
      this.enqueueTime = enqueueTime;
      this.processingTime = processingTime;
    }
  }
  public int[] getOrder(int[][] tasks) {
    int len = tasks.length;
    List taskList = new ArrayList<>();
    for(int i=0; i t1.enqueueTime - t2.enqueueTime);
    //利用最小堆获取下个要执行的任务
    PriorityQueue minHeap = new PriorityQueue<>((t1,t2) -> {
      if(t1.processingTime == t2.processingTime){
        //当执行时间相同时,根据id升序
        return t1.id - t2.id;
      }else{
        //当执行时间不同时,根据执行时间升序
        return t1.processingTime - t2.processingTime;
      }
    });
    long now = 0;//当前时间,使用long防止int溢出
    int i = 0;//taskList的坐标
    int[] ret = new int[len];
    int p = 0;//ret的坐标
    while(i

你可能感兴趣的:(算法)