五、发LeetCoin(LCP5)

题目描述:
力扣决定给一个刷题团队发LeetCoin作为奖励。同时,为了监控给大家发了多少LeetCoin,力扣有时候也会进行查询。

该刷题团队的管理模式可以用一棵树表示:

团队只有一个负责人,编号为1。除了该负责人外,每个人有且仅有一个领导(负责人没有领导);
不存在循环管理的情况,如A管理B,B管理C,C管理A。

力扣想进行的操作有以下三种:

给团队的一个成员(也可以是负责人)发一定数量的LeetCoin;
给团队的一个成员(也可以是负责人),以及他/她管理的所有人(即他/她的下属、他/她下属的下属,……),发一定数量的LeetCoin;
查询某一个成员(也可以是负责人),以及他/她管理的所有人被发到的LeetCoin之和。

输入:

N表示团队成员的个数(编号为1~N,负责人为1);
leadership是大小为(N - 1) * 2的二维数组,其中每个元素[a, b]代表b是a的下属;
operations是一个长度为Q的二维数组,代表以时间排序的操作,格式如下:
operations[i][0] = 1: 代表第一种操作,operations[i][1]代表成员的编号,operations[i][2]代表LeetCoin的数量;
operations[i][0] = 2: 代表第二种操作,operations[i][1]代表成员的编号,operations[i][2]代表LeetCoin的数量;
operations[i][0] = 3: 代表第三种操作,operations[i][1]代表成员的编号;
输出:

返回一个数组,数组里是每次查询的返回值(发LeetCoin的操作不需要任何返回值)。由于发的LeetCoin很多,请把每次查询的结果模1e9+7 (1000000007)。

示例 1:

输入:N = 6, leadership = [[1, 2], [1, 6], [2, 3], [2, 5], [1, 4]], operations = [[1, 1, 500], [2, 2, 50], [3, 1], [2, 6, 15], [3, 1]]
输出:[650, 665]
解释:团队的管理关系见下图。
第一次查询时,每个成员得到的LeetCoin的数量分别为(按编号顺序):500, 50, 50, 0, 50, 0;
第二次查询时,每个成员得到的LeetCoin的数量分别为(按编号顺序):500, 50, 50, 0, 50, 15.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/coin-bonus
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

在这里插入图片描述
限制:

1 <= N <= 50000
1 <= Q <= 50000
operations[i][0] != 3 时,1 <= operations[i][2] <= 5000

我不知道这个思路错在哪里,哎,调整了半天还是不对。。。
一般的测试用例都能通过。。

class Solution {
 
  //节点1为父节点
    class Node {
        int x;
//        保存父节点
        Node p;
//        保存下属和自己的硬币数量之和
        long coinshu = 0;
        // 所有的硬币
        int coin = 0;
        List<Node> list = new ArrayList<>();
        public Node(int x) {
            this.x = x;
        }
    }
    public int[] bonus(int n, int[][] leadership, int[][] operations) {
        int mod = 1000000007;
//            保存所有需要的
        List<Integer> list = new ArrayList<>();
        Node node [] = new Node[n + 1];
        for (int i = 0; i < node.length; i++) {
            node[i] = new Node(i);
        }
        Map<Integer, List<Node>> map = new HashMap<>();
//        初始化领导map
        for (int[] ints : leadership) {
           int leader = ints[0];
           int beleader = ints[1];
//           父节点初始化
           node[beleader].p = node[leader];
//           孩子节点初始化
           map.putIfAbsent(leader,new ArrayList<>());
           map.get(leader).add(node[beleader]);
        }
        for (int i = 0; i < operations.length; i++) {
            int operation = operations[i][0];
            if(operation == 2){
//情况二 发数量的币
                int people = operations[i][1];
                int get = operations[i][2];
                List<Node> temlist = map.get(people);
                Queue<List<Node>> queue = new LinkedList();
                queue.offer(temlist);
                node[people].coin += get;
                long x = get;
                while (!queue.isEmpty()){
                    List<Node> tem = queue.poll();
                    if(tem != null){
                        for (Node node1 : tem) {
                            node1.coin += get;
                            node1.coin %= mod;
                            x += get;
                            x %= mod;
                            if(map.get(node1.x) != null){
                                queue.offer(map.get(node1.x));
                            }
                        }
                    }
                }
//                更新coinshu这个属性
                node[people].coinshu = (node[people].coinshu + x) % mod;
//                更新父节点的coinshu
                while (node[people].p != null){
                    node[people].p.coinshu = (node[people].p.coinshu + x) %mod;
                    people = node[people].p.x;
                }
            }else if(operation == 1){
                int people = operations[i][1];
                int get = operations[i][2];
                node[people].coin += get;
                node[people].coin %= mod;
                node[people].coinshu = (node[people].coinshu +get) % mod;
                while (node[people].p != null){
                    node[people].p.coinshu = (node[people].p.coinshu + get) %mod;
                    people = node[people].p.x;
                }
            }else{
//                查询这种情况
                int people = operations[i][1];
                list.add((int) (node[people].coinshu % mod));
            }
        }
        int result[] = new int[list.size()];
        for (int i = 0; i < result.length; i++) {
            result[i] = list.get(i);
        }
        return result;
    }

}

看看别人实现的

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

class Solution {
    public int[] bonus(int n, int[][] leadership, int[][] operations) {

        HashMap<Integer, Node> map = new HashMap<>();
        for(int i=0; i<n; i++) 
            map.put(i+1, new Node(i+1));
        for (int[] ints : leadership) {
            Node c = map.get(ints[1]);
            Node p = map.get(ints[0]);
            c.p = p;
            p.child.add(c);
        }

        LinkedList<Integer> rs = new LinkedList<>();
        for (int[] operation : operations) {

            Node node = map.get(operation[1]);
            if(operation[0] == 1) {

                node.n += operation[2];
                Node t = node;
                while(t.p != null) {
                    t.p.n += operation[2];
                    t = t.p;
                }
            } else if (operation[0] == 2) {
                int update = update(node, operation[2]);
                while(node.p != null) {
                    node.p.n += update;
                    node = node.p;
                }
            } else {

                rs.add((int)(node.n%1000000007));
            }
        }
        Iterator<Integer> iterator = rs.iterator();
        int[] arr = new int[rs.size()];
        for(int i=0; iterator.hasNext(); i++) {
            arr[i] = iterator.next();
        }
        return arr;
    }

    int update(Node node, int c) {
        if(node == null)
            return 0;
        int n = c;
        for (Node ch : node.child) {
            n += update(ch, c);
        }
        node.n += n;
        return n;
    }

    class Node {
        int num;
        int n;
        Node p;
        LinkedList<Node> child = new LinkedList<>();

        public Node(int num) {
            this.num = num;
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值