Blage's Coding Blage's Coding
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
Home
算法
  • 手写Spring
  • SSM
  • SpringBoot
  • JavaWeb
  • JAVA基础
  • 容器
  • Netty

    • IO模型
    • Netty初级
    • Netty原理
  • JVM
  • JUC
  • Redis基础
  • 源码分析
  • 实战应用
  • 单机缓存
  • MySQL

    • 基础部分
    • 实战与处理方案
    • 面试
  • ORM框架

    • Mybatis
    • Mybatis_Plus
  • SpringCloudAlibaba
  • MQ消息队列
  • Nginx
  • Elasticsearch
  • Gateway
  • Xxl-job
  • Feign
  • Eureka
  • 面试
  • 工具
  • 项目
  • 关于
🌏本站
🧸GitHub (opens new window)
  • 数组

  • 链表

  • 字符串

  • 二叉树

  • 动态规划

  • 深搜回溯

  • 数学贪心

  • 堆栈队列

  • 前缀和

  • 算法设计

    • 146.LRU缓存
    • 207. 拓扑排序
    • 10. 正则表达式匹配
    • 208. 前缀树
    • 2699. Dijkstra
    • 1483. 二进制倍增算法
    • 155. 最小栈
    • 2761. 欧拉线性筛
    • 297. 二叉树的序列化与反序列化
    • 28. KMP算法
    • 232. 用栈实现队列
    • 127. BFS广度优先搜索
    • 449. 序列化和反序列化二叉搜索树
    • 1462. floyed根可达性算法
    • 2603. 节点出入度数组
    • 460. LFU 缓存
    • 1993. 树上的操作
      • 1.哈希
    • 剑指 Offer 41. 数据流中的中位数
    • 剑指 Offer 59 - II. 队列的最大值
  • 位运算

  • WA

  • 算法
  • 算法设计
phan
2023-09-25
目录

1993. 树上的操作

# 1993. 树上的操作 (opens new window)

# 1.哈希

难点在于update实现,需要遍历当前节点的祖先节点和子树。其中祖先节点直接根据parent数组遍历;子树则需要建立哈希表保存节点的孩子节点,递归map的子节点。

class LockingTree {
    private static class Node{
        int userId;
        int parentId;
        boolean lock;
    }
    Node[] tree;
    Map<Integer,List<Integer>> map;
    public LockingTree(int[] parent) {
        map=new HashMap<>();
        tree=new Node[parent.length];
        for(int i=0;i<parent.length;i++){
            tree[i]=new Node();
            tree[i].parentId=parent[i];
            if(map.containsKey(parent[i])){
                map.get(parent[i]).add(i);
            }
            else{
                List<Integer> list = new ArrayList<>();
                list.add(i);
                map.put(parent[i], list);
            }
        }
    }
    public boolean lock(int num, int user) {
        if(!tree[num].lock){
            tree[num].lock=true;
            tree[num].userId=user;
            return true;
        }
        else{
            return false;
        }
    }
    public boolean unlock(int num, int user) {
        if(tree[num].lock&&tree[num].userId==user){
            tree[num].lock=false;
            return true;
        }
        else return false;
    }
    public boolean upgrade(int num, int user) {
        int father=tree[num].parentId;
        boolean checkFather=false;
        while(father!=-1){
            if(tree[father].lock){
                checkFather=true;
                break;
            }
            father=tree[father].parentId;
        }
        boolean checkSon=map.containsKey(num)?check(num):false;
        if(checkFather||tree[num].lock||!checkSon) return false;

        release(num);
        tree[num].userId=user;
        tree[num].lock=true;
        return true;

    }
    public boolean check(int num){
        for(Integer i:map.get(num)){
            if(tree[i].lock)return true;
            if(map.containsKey(i)&&check(i)) return true;
        }
        return false;
    }
    public void release(int num){
        for(Integer i:map.get(num)){
            tree[i].lock=false;
            if(map.containsKey(i)) release(i);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
编辑 (opens new window)
#Leetcode#算法设计
上次更新: 2023/12/15, 15:49:57
460. LFU 缓存
剑指 Offer 41. 数据流中的中位数

← 460. LFU 缓存 剑指 Offer 41. 数据流中的中位数→

Theme by Vdoing | Copyright © 2023-2024 blageCoder
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式