zoukankan      html  css  js  c++  java
  • 派对的最大快乐值


    import java.util.ArrayList;
    import java.util.List;

    /**
    * 派对的最大快乐值
    * <p>
    * 一棵多叉树代表员工的上下级关系,孩子节点是父节点的直接下级。
    * 节点代表员工,属性包括快乐值和孩子节点列表。
    * 大家参加了party,要求一个员工去了则它的所有直接下级都不能去,问参加party能得到的最大快乐值是多少。
    */
    public class MaxHappy {

    public static int maxHappy(Employee boss) {
    if (boss == null) {
    return 0;
    }
    ResultInfo all = process(boss);
    return Math.max(all.yes, all.no);
    }

    public static ResultInfo process(Employee x) {
    if (x.nexts.isEmpty()) {
    return new ResultInfo(x.happy, 0);
    }
    int yes = x.happy;
    int no = 0;
    for (Employee next : x.nexts) {
    ResultInfo nextInfo = process(next);
    yes += nextInfo.no;
    no += Math.max(nextInfo.yes, nextInfo.no);
    }
    return new ResultInfo(yes, no);
    }

    /**
    * 返回的快乐值
    */
    public static class ResultInfo {

    // 参与的快乐值
    public int yes;

    // 不参与的快乐值
    public int no;

    public ResultInfo(int y, int n) {
    yes = y;
    no = n;
    }

    }

    /**
    * 员工
    */
    public static class Employee {

    // 快乐值
    public int happy;

    // 直接下级
    public List<Employee> nexts;

    public Employee(int h) {
    happy = h;
    nexts = new ArrayList<>();
    }

    }

    }

    /* 如有意见或建议,欢迎评论区留言;如发现代码有误,欢迎批评指正 */
  • 相关阅读:
    第一次的作业
    第02组 Alpha冲刺(2/4)
    团队项目选题报告
    第一次个人编程作业
    第二次结对编程作业
    胖子的故事(四)
    关于博客园的聚合问题
    blog是写给谁看的
    ASP.NET Forms 身份验证
    要努力了!
  • 原文地址:https://www.cnblogs.com/laydown/p/12977391.html
Copyright © 2011-2022 走看看