栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

力扣:731. 我的日程安排表 II

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

力扣:731. 我的日程安排表 II


import java.util.*;


public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        MyCalendarTwo2 my = new MyCalendarTwo2();
        System.out.println(my.book(10, 20));
        System.out.println(my.book(50, 60));
        System.out.println(my.book(10, 40));
        System.out.println(my.book(5, 15));
        System.out.println(my.book(5, 10));
        System.out.println(my.book(25, 55));
    }


}


class MyCalendarTwo {

    List booked; // 已经预定的
    List overlaps; // 重叠的
    public MyCalendarTwo() {
        booked = new ArrayList<>();
        overlaps = new ArrayList<>();
    }

    public boolean book(int start, int end) {
        for (int[] overlap : overlaps) {
            int left = overlap[0], right = overlap[1];
            // 存在这个区间
            if (left < end && start < right){
                return false;
            }
        }

        for (int[] ints : booked) {
            int left = ints[0], right = ints[1];
            if (left < end &&  start < right){
                overlaps.add(new int[]{Math.max(left, start), Math.min(right, end)});
            }
        }
        booked.add(new int[]{start, end});

        return true;
    }
}

// 分叉数组
class MyCalendarTwo2 {

    TreeMap treeMap;

    public MyCalendarTwo2() {
        treeMap = new TreeMap<>();
    }

    public boolean book(int start, int end) {
        int ans = 0;
        int maxBook = 0;
        treeMap.put(start, treeMap.getOrDefault(start, 0) + 1);
        treeMap.put(end, treeMap.getOrDefault(end, 0) - 1);
        for (Map.Entry entry : treeMap.entrySet()) {
            Integer value = entry.getValue();
            maxBook += value;
            ans = Math.max(maxBook, ans);
            if (maxBook > 2){
                // 去掉当前结果
                treeMap.put(start, treeMap.getOrDefault(start, 0) - 1);
                treeMap.put(end, treeMap.getOrDefault(end, 0) + 1);
                return false;
            }
        }
        return  true;
    }
}


class MyCalendarTwo3 {

    class Node{
        Node ls, rs;
        int max, add;
    }

    int N = (int)1e9;
    Node root= new Node();

    void update(Node node, int lc, int rc, int l, int r, int v){
        // 到达根节点
        if (l <= lc && rc <= r ){
            node.add += v;
            node.max += v;
            return;
        }

        pushdown(node);
        int mid = lc + rc >> 1;
        // 开左边的点
        if (l <= mid){
            update(node.ls, lc, mid, l,  r, v);
        }

        // 开右边的点
        if (r > mid){
            update(node.rs, mid + 1, rc, l, r, v);
        }
        pushup(node);
    }

    int query(Node node, int lc, int rc, int l, int r){
        // 找到了结果
        if (l <= lc && rc <= r){
            return node.max;
        }
        pushdown(node);
        int mid = lc + rc >> 1, ans = 0;
        if (l <= mid){
            ans = query(node.ls, lc, mid, l, r);
        }

        if (r > mid){
            ans = Math.max(query(node.rs, mid + 1, rc, l, r), ans );
        }
        return ans;
    }

    void pushdown(Node node){
        if (node.ls == null){
            node.ls = new Node();
        }
        if (node.rs == null){
            node.rs = new Node();
        }

        // 懒加载因子
        int add = node.add;
        node.ls.max += add;
        node.rs.max += add;
        node.ls.add += add;
        node.rs.add += add;
        // 删除加载因子
        node.add = 0;
    }

    void pushup(Node node){
        node.max = Math.max(node.ls.max, node.rs.max);
    }

    public boolean book(int start, int end) {
        if (query(root, 0, N, start, end -1) >= 2){
            return false;
        }
        update(root, 0, N, start, end - 1, 1);
        return true;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1018830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号