LeetCode 732 - My Calendar III


Related: LeetCode 729 - My Calendar I
LeetCode 731 - My Calendar II
LeetCode 732 - My Calendar III
https://leetcode.com/problems/my-calendar-iii/description/
Implement a MyCalendarThree class to store your events. A new event can always be added.
Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
K-booking happens when K events have some non-empty intersection (ie., there is some time that is common to all K events.)
For each call to the method MyCalendar.book, return an integer K representing the largest integer such that there exists a K-booking in the calendar.
Your class will be called like this: MyCalendarThree cal = new MyCalendarThree(); MyCalendarThree.book(start, end)
Example 1:
MyCalendarThree();
MyCalendarThree.book(10, 20); // returns 1
MyCalendarThree.book(50, 60); // returns 1
MyCalendarThree.book(10, 40); // returns 2
MyCalendarThree.book(5, 15); // returns 3
MyCalendarThree.book(5, 10); // returns 3
MyCalendarThree.book(25, 55); // returns 3
Explanation: 
The first two events can be booked and are disjoint, so the maximum K-booking is a 1-booking.
The third event [10, 40) intersects the first event, and the maximum K-booking is a 2-booking.
The remaining events cause the maximum K-booking to be only a 3-booking.
Note that the last event locally causes a 2-booking, but the answer is still 3 because
eg. [10, 20), [10, 40), and [5, 15) are still triple booked.
Note:


  • The number of calls to MyCalendarThree.book per test case will be at most 400.
  • In calls to MyCalendarThree.book(start, end)start and end are integers in the range [0, 10^9].

  • X. Boundary Count
    1. 这里用mapvector更加合适, 因为这里的线段的两个端点具有强离散性, 这也是线段树相比于用map不如的地方。

    换一种想法: 如果只是保存起始和终点的情况,而完全不管中间经过的点呢?
    类似于图, 线段可以看做是一条有向边, 每次从一个点A指向B,相当于A的出度+1, B的入度 + 1。
    这种情况下, 找到整个数轴上出度最大的点就是被覆盖次数最多的点。
    这道题的做法十分的棒,是图的出度入度的一个很好的变形,很值得学习

    http://leungyukshing.cn/archives/LeetCode%E8%A7%A3%E9%A2%98%E6%8A%A5%E5%91%8A%EF%BC%88%E5%85%AD%EF%BC%89--%20732.%20My%20Calendar%20III.html

    https://leetcode.com/problems/my-calendar-iii/discuss/109556/JavaC%2B%2B-Clean-Code
    This is to find the maximum number of concurrent ongoing event at any time.
    We can log the start & end of each event on the timeline, each start add a new ongoing event at that time, each end terminate an ongoing event. Then we can scan the timeline to figure out the maximum number of ongoing event at any time.
    The most intuitive data structure for timeline would be array, but the time spot we have could be very sparse, so we can use sorted map to simulate the time line to save space.
        private TreeMap<Integer, Integer> timeline = new TreeMap<>();
        public int book(int s, int e) {
            timeline.put(s, timeline.getOrDefault(s, 0) + 1); // 1 new event will be starting at [s]
            timeline.put(e, timeline.getOrDefault(e, 0) - 1); // 1 new event will be ending at [e];
            int ongoing = 0, k = 0;
            for (int v : timeline.values())
                k = Math.max(k, ongoing += v);
            return k;
        }

    When booking a new event [start, end), count delta[start]++ and delta[end]--. When processing the values of delta in sorted order of their keys, the largest such value is the answer.
    In Python, we sort the set each time instead, as there is no analog to TreeMap available.
    • Time Complexity: O(N^2), where N is the number of events booked. For each new event, we traverse delta in O(N) time. In Python, this is O(N^2 \log N) owing to the extra sort step.
    • Space Complexity: O(N), the size of delta.
        TreeMap<Integer, Integer> delta;

        public MyCalendarThree() {
            delta = new TreeMap();
        }

        public int book(int start, int end) {
            delta.put(start, delta.getOrDefault(start, 0) + 1);
            delta.put(end, delta.getOrDefault(end, 0) - 1);

            int active = 0, ans = 0;
            for (int d: delta.values()) {
                active += d;
                if (active > ans) ans = active;
            }
            return ans;
        }

    https://leetcode.com/problems/my-calendar-iii/discuss/109556/JavaC++-Clean-Code
    This is to find the maximum number of concurrent ongoing event at any time.
    We can log the start & end of each event on the timeline, each start add a new ongoing event at that time, each end terminate an ongoing event. Then we can scan the timeline to figure out the maximum number of ongoing event at any time.
    The most intuitive data structure for timeline would be array, but the time spot we have could be very sparse, so we can use sorted map to simulate the time line to save space.
    Java
        private TreeMap<Integer, Integer> timeline = new TreeMap<>();
        public int book(int s, int e) {
            timeline.put(s, timeline.getOrDefault(s, 0) + 1); // 1 new event will be starting at [s]
            timeline.put(e, timeline.getOrDefault(e, 0) - 1); // 1 new event will be ending at [e];
            int ongoing = 0, k = 0;
            for (int v : timeline.values())
                k = Math.max(k, ongoing += v);
            return k;
        }
    
    X. Binary Search Tree
    https://www.cnblogs.com/FannyChung/p/7896415.html
    和第一题一样使用BST,没有重叠的区间的节点操作类似第一题,但是对于有重叠区间的节点,要进行分裂,把lser,lsre,slre,sler四种情况总结起来就是中间两个值作为当前节点的起始和终止时间,且次数要增加,两侧分别进行左递归和右递归,次数根据lr还是se再外侧来决定。【selr分别为待插入的start,end,当前节点的left和right】
    注意,次数不能简单的为1,对于分裂了lr的情况(如lser和lsre、sler),递归的时候次数可能要指定为当前节点的已有次数,而这个不是固定为1的。所以插入次数也要作为参数进行传递。
        class Node {//节点有起始终止事件,左右子节点,这个时间区间的重叠次数
            int left, right;
            int count;
            Node leftChild, rightChild;
    
            public Node(int l, int r, int c) {
                left = l;
                right = r;
                count = c;
            }
        }
    
        int maxK = 1;//只要调用1次book,则最大记录至少为1,所以可以直接初始化为1
        Node root;
    
        public int book(int start, int end) {
            root = insert(root, start, end, 1);
            return maxK;
        }
    
        private Node insert(Node root2, int start, int end, int c) {//由于需要修改节点的链接关系,所以需要返回节点
            if (start >= end) {// no need to take action
                return root2;
            }
            if (root2 == null) {
                root2 = new Node(start, end, c);
                return root2;
            }
            int l = root2.left;
            int r = root2.right;
            if (end <= l) {//一定落在当前节点的左侧即左子树上,进行左递归
                root2.leftChild = insert(root2.leftChild, start, end, c);
            } else if (start >= r) {
                root2.rightChild = insert(root2.rightChild, start, end, c);
            } else {
                int[] a = new int[4];//给四个值排序
                if (start <= l) {
                    a[0] = start;
                    a[1] = l;
                } else {
                    a[0] = l;
                    a[1] = start;
                }
                if (end <= r) {
                    a[2] = end;
                    a[3] = r;
                } else {
                    a[2] = r;
                    a[3] = end;
                }
                root2.left = a[1];//中间的两个值作为当前节点的值
                root2.right = a[2];
    
                root2.leftChild = insert(root2.leftChild, a[0], a[1], start <= l ? c : root2.count);//左递归,如果start在外侧,则次数为c;如果l在外侧,则次数为当前节点的次数
                root2.rightChild = insert(root2.rightChild, a[2], a[3], end >= r ? c : root2.count);
                root2.count += c;//当前节点的次数要增加,并且根据大小情况选择性的更新maxK
                maxK = Math.max(maxK, root2.count);
            }
            return root2;
        }
    https://blog.csdn.net/laputafallen/article/details/79676753
    https://leetcode.com/problems/my-calendar-iii/discuss/109575/Java-O(n)-BST-Method
    O(N)

    class Node{ private int k, v; private Node left, right; public Node(int k, int v) { this.k = k; this.v = v; } } private Node root; private int curt, count; public MyCalendarThree() { } private Node insert(Node node, int k, int v) { if (node == null) { node = new Node(k, v); return node; } else if (node.k == k) { node.v += v; } else if (node.k < k) { node.right = insert(node.right, k, v); } else { node.left = insert(node.left, k, v); } return node; } private void count(Node node) { if (node == null) { return; } count(node.left); curt += node.v; count = Math.max(count, curt); count(node.right); } public int book(int start, int end) { root = insert(root, start, 1); root = insert(root, end, -1); curt = count = 0; count(root); return count; }

    X. Segment Tree

    http://hehejun.blogspot.com/2018/07/leetcodemy-calendar-iii.html
    此外,这也是一道很明显的range query的题目,我们可以用segment tree来解决。具体来说,叶节点存的是当前index对应被覆盖了多少次,非叶节点存的就是这个区间对应的最多的被覆盖的次数。每次插入[s , e]的话就相当于对[s , e]进行range update,区间里的每一个元素加1。区间更新我们用lazy propagation,具体参考上面给出的链接。查询的话直接查根节点储存的信息即可。时间复杂度O(log n),空间复杂度O(n)
    https://blog.csdn.net/Guo15331092/article/details/78883265
    线段树放在这道题上其实非常显然, 只需要简单的调用几次线段树的方法就可以得到结果。大致如下:

    每次插入一条线段, 对应更新线段树中原先线段上的值。这里,线段的值就是该线段出现的次数。
    用一个变量记录最大值。每次在插入线段时维护这个最大值。 插入完之后最大值即为当次结果返回
    以上就是线段树的做法。这个做法的缺点如下:
    1. 线段树写起来繁琐。 这个写过的自然懂。
    2. 线段树的范围太大。 注意到这道题的数据范围对朴素线段树是十分不友好的: 插入次数 < 400, 而插入范围是109109 直接储存直接爆内存应该毫无疑问。 显然要做压缩。
    3. 由于是动态查询, 我们不能事先知道线段的起始与终点, 因此压缩比较麻烦。。。

    https://leetcode.com/problems/my-calendar-iii/discuss/109568/Java-Solution-O(n-log(len))-beats-100-Segment-Tree
    This solution is basically a segment tree solution.
    The qurey MyCalendarThree.book(start, end) can be treated as for(i = start; i < end; i++) nums[i] += 1. And the request becomes "find the maximum nums[i]".

    To query a range maximum, we process at most two nodes at every level and number of levels is O(logn)1. Thus the overall complexity is O(n log(len)), where len is the length of this segment (ie. 10^9 in this problem).
    class MyCalendarThree {
     SegmentTree segmentTree;
        public MyCalendarThree() {
         segmentTree = new SegmentTree(0, 1000000000);
        }
        public int book(int start, int end) {
            segmentTree.add(start, end, 1);
            return segmentTree.getMax();
        }
    }
    
    class SegmentTree {
        TreeNode root;
        public SegmentTree(int left, int right) {
            root = new TreeNode(left, right);
        }
        public void add(int start, int end, int val) {
            TreeNode event = new TreeNode(start, end);
         add(root, event, val);
        }
        private void add(TreeNode root, TreeNode event, int val) {
            if(root == null) {
                return ;
            }
            /**
             * If current node's range lies completely in update query range.
             */
            if(root.inside(event)) {
                root.booked += val;
                root.savedres += val;
            }
            /**
             * If current node's range overlaps with update range, follow the same approach as above simple update.
             */
            if(root.intersect(event)) {
             // Recur for left and right children.
                int mid = (root.start + root.end) / 2;
                if(root.left == null) {
                    root.left = new TreeNode(root.start, mid);
                }
                add(root.left, event, val);
                if(root.right == null) {
                    root.right = new TreeNode(mid, root.end);
                }
                add(root.right, event, val);
                // Update current node using results of left and right calls.
                root.savedres = Math.max(root.left.savedres, root.right.savedres) + root.booked;
            }
        }
        public int getMax() {
            return root.savedres;
        }
        /**
         * find maximum for nums[i] (start <= i <= end) is not required.
         * so i did not implement it. 
         */
        public int get(int start, int right) {return 0;}
     class TreeNode {
         int start, end;
         TreeNode left = null, right = null;
         /**
          * How much number is added to this interval(node)
          */
         int booked = 0;
         /**
          * The maximum number in this interval(node). 
          */
         int savedres = 0;
         public TreeNode(int s, int t) {
             this.start = s;
             this.end = t;
         }
         public boolean inside(TreeNode b) {
             if(b.start <= start && end <= b.end) {
                 return true;
             }
             return false;
         }
         public boolean intersect(TreeNode b) {
          if(inside(b) || end <= b.start || b.end <= start) {
                 return false;
             }
             return true;
         }
     }
    }
    
    Time Complexity:
    To query a range maximum, we process at most two nodes at every level and number of levels is O(logn)1. Thus the overall complexity is O(n log(len)), where len is the length of this segment (ie. 10^9 in this problem
    https://leetcode.com/problems/my-calendar-iii/discuss/109569/Question-explanation-please
    The problem asks you to return an integer K representing the largest integer such that there exists a K-booking in the calendar, i.e., the global max number of overlaps. A K-booking happens when there is some time that is common to K events.
    1. In the example when you book (5, 10), the booking itself only causes 2-booking locally, but the global max is still three (made by (10,20), (10,40), (5,15)).
    2. (25,55) causes 2-booking with (10,40) or (50,60), while the global max is still three (made by (10,20), (10,40), (5,15)).
    X. https://www.acwing.com/solution/LeetCode/content/183/
    (端点排序) O(n2)O(n2)
    将一个区间拆分为两个端点,并且标记左端点还是右端点。
    将这些端点存入一个数组中,记录两个值,一个是这个端点是否是左右端点,一个是这个端点的值。
    并且在插入的过程中保持这个数组有序(插入排序)。

    接下来维护一个当前区间的最大值
    扫描整个数组,碰到左端点值加一,碰到右端点值减一(用纸笔画一下就明白了)。
    最后返回最大值。

    时间复杂度分析:一共n次插入,每次插入两个元素,并且维持这个数组有序,并且每次都要扫描这个数组。
    n次操作,每次操作都需要 O(n)O(n)的复杂度。一共是 O(n2)O(n2)的复杂度
    struct node{
        int value;
        bool isleft;
    };

    bool cmp(node l,node r){
        if (l.value == r.value )
            return l.isleft?true:false;
        return l.value < r. value;
    }
    class MyCalendarThree {
    public:
        vector<node> res;
        MyCalendarThree() {
            res.resize(0);
        }

        int book(int start, int end) {
            node cur1;
            node cur2;

            cur1.isleft = true; cur1.value = start;

            cur2.isleft = false;cur2.value = end-1;
            int sz = res.size();
            for (int i = 0;i<res.size();++i){
                if (cmp(cur1,res[i])){
                    res.insert(res.begin()+i,cur1);
                    break;
                }
            }
            if (res.size() != sz+1)
                res.push_back(cur1);
            sz = res.size();
            for (int i = 0;i<res.size();++i){
                if (cmp(cur2,res[i])){
                    res.insert(res.begin()+i,cur2);
                    break;
                }
            }
            if (res.size()!= sz+1)
                res.push_back(cur2);


            int cnt = 0;
            int maxres = 0;
            for (int i = 0;i<res.size();++i){
                if (res[i].isleft) {
                    cnt++;
                    maxres = max(maxres,cnt);
                }
                else
                    cnt--;
            }


            return maxres;


        }
    };

    Labels

    LeetCode (1432) GeeksforGeeks (1122) LeetCode - Review (1067) Review (882) Algorithm (668) to-do (609) Classic Algorithm (270) Google Interview (237) Classic Interview (222) Dynamic Programming (220) DP (186) Bit Algorithms (145) POJ (141) Math (137) Tree (132) LeetCode - Phone (129) EPI (122) Cracking Coding Interview (119) DFS (115) Difficult Algorithm (115) Lintcode (115) Different Solutions (110) Smart Algorithm (104) Binary Search (96) BFS (91) HackerRank (90) Binary Tree (86) Hard (79) Two Pointers (78) Stack (76) Company-Facebook (75) BST (72) Graph Algorithm (72) Time Complexity (69) Greedy Algorithm (68) Interval (63) Company - Google (62) Geometry Algorithm (61) Interview Corner (61) LeetCode - Extended (61) Union-Find (60) Trie (58) Advanced Data Structure (56) List (56) Priority Queue (53) Codility (52) ComProGuide (50) LeetCode Hard (50) Matrix (50) Bisection (48) Segment Tree (48) Sliding Window (48) USACO (46) Space Optimization (45) Company-Airbnb (41) Greedy (41) Mathematical Algorithm (41) Tree - Post-Order (41) ACM-ICPC (40) Algorithm Interview (40) Data Structure Design (40) Graph (40) Backtracking (39) Data Structure (39) Jobdu (39) Random (39) Codeforces (38) Knapsack (38) LeetCode - DP (38) Recursive Algorithm (38) String Algorithm (38) TopCoder (38) Sort (37) Introduction to Algorithms (36) Pre-Sort (36) Beauty of Programming (35) Must Known (34) Binary Search Tree (33) Follow Up (33) prismoskills (33) Palindrome (32) Permutation (31) Array (30) Google Code Jam (30) HDU (30) Array O(N) (29) Logic Thinking (29) Monotonic Stack (29) Puzzles (29) Code - Detail (27) Company-Zenefits (27) Microsoft 100 - July (27) Queue (27) Binary Indexed Trees (26) TreeMap (26) to-do-must (26) 1point3acres (25) GeeksQuiz (25) Merge Sort (25) Reverse Thinking (25) hihocoder (25) Company - LinkedIn (24) Hash (24) High Frequency (24) Summary (24) Divide and Conquer (23) Proof (23) Game Theory (22) Topological Sort (22) Lintcode - Review (21) Tree - Modification (21) Algorithm Game (20) CareerCup (20) Company - Twitter (20) DFS + Review (20) DP - Relation (20) Brain Teaser (19) DP - Tree (19) Left and Right Array (19) O(N) (19) Sweep Line (19) UVA (19) DP - Bit Masking (18) LeetCode - Thinking (18) KMP (17) LeetCode - TODO (17) Probabilities (17) Simulation (17) String Search (17) Codercareer (16) Company-Uber (16) Iterator (16) Number (16) O(1) Space (16) Shortest Path (16) itint5 (16) DFS+Cache (15) Dijkstra (15) Euclidean GCD (15) Heap (15) LeetCode - Hard (15) Majority (15) Number Theory (15) Rolling Hash (15) Tree Traversal (15) Brute Force (14) Bucket Sort (14) DP - Knapsack (14) DP - Probability (14) Difficult (14) Fast Power Algorithm (14) Pattern (14) Prefix Sum (14) TreeSet (14) Algorithm Videos (13) Amazon Interview (13) Basic Algorithm (13) Codechef (13) Combination (13) Computational Geometry (13) DP - Digit (13) LCA (13) LeetCode - DFS (13) Linked List (13) Long Increasing Sequence(LIS) (13) Math-Divisible (13) Reservoir Sampling (13) mitbbs (13) Algorithm - How To (12) Company - Microsoft (12) DP - Interval (12) DP - Multiple Relation (12) DP - Relation Optimization (12) LeetCode - Classic (12) Level Order Traversal (12) Prime (12) Pruning (12) Reconstruct Tree (12) Thinking (12) X Sum (12) AOJ (11) Bit Mask (11) Company-Snapchat (11) DP - Space Optimization (11) Dequeue (11) Graph DFS (11) MinMax (11) Miscs (11) Princeton (11) Quick Sort (11) Stack - Tree (11) 尺取法 (11) 挑战程序设计竞赛 (11) Coin Change (10) DFS+Backtracking (10) Facebook Hacker Cup (10) Fast Slow Pointers (10) HackerRank Easy (10) Interval Tree (10) Limited Range (10) Matrix - Traverse (10) Monotone Queue (10) SPOJ (10) Starting Point (10) States (10) Stock (10) Theory (10) Tutorialhorizon (10) Kadane - Extended (9) Mathblog (9) Max-Min Flow (9) Maze (9) Median (9) O(32N) (9) Quick Select (9) Stack Overflow (9) System Design (9) Tree - Conversion (9) Use XOR (9) Book Notes (8) Company-Amazon (8) DFS+BFS (8) DP - States (8) Expression (8) Longest Common Subsequence(LCS) (8) One Pass (8) Quadtrees (8) Traversal Once (8) Trie - Suffix (8) 穷竭搜索 (8) Algorithm Problem List (7) All Sub (7) Catalan Number (7) Cycle (7) DP - Cases (7) Facebook Interview (7) Fibonacci Numbers (7) Flood fill (7) Game Nim (7) Graph BFS (7) HackerRank Difficult (7) Hackerearth (7) Inversion (7) Kadane’s Algorithm (7) Manacher (7) Morris Traversal (7) Multiple Data Structures (7) Normalized Key (7) O(XN) (7) Radix Sort (7) Recursion (7) Sampling (7) Suffix Array (7) Tech-Queries (7) Tree - Serialization (7) Tree DP (7) Trie - Bit (7) 蓝桥杯 (7) Algorithm - Brain Teaser (6) BFS - Priority Queue (6) BFS - Unusual (6) Classic Data Structure Impl (6) DP - 2D (6) DP - Monotone Queue (6) DP - Unusual (6) DP-Space Optimization (6) Dutch Flag (6) How To (6) Interviewstreet (6) Knapsack - MultiplePack (6) Local MinMax (6) MST (6) Minimum Spanning Tree (6) Number - Reach (6) Parentheses (6) Pre-Sum (6) Probability (6) Programming Pearls (6) Rabin-Karp (6) Reverse (6) Scan from right (6) Schedule (6) Stream (6) Subset Sum (6) TSP (6) Xpost (6) n00tc0d3r (6) reddit (6) AI (5) Abbreviation (5) Anagram (5) Art Of Programming-July (5) Assumption (5) Bellman Ford (5) Big Data (5) Code - Solid (5) Code Kata (5) Codility-lessons (5) Coding (5) Company - WMware (5) Convex Hull (5) Crazyforcode (5) DFS - Multiple (5) DFS+DP (5) DP - Multi-Dimension (5) DP-Multiple Relation (5) Eulerian Cycle (5) Graph - Unusual (5) Graph Cycle (5) Hash Strategy (5) Immutability (5) Java (5) LogN (5) Manhattan Distance (5) Matrix Chain Multiplication (5) N Queens (5) Pre-Sort: Index (5) Quick Partition (5) Quora (5) Randomized Algorithms (5) Resources (5) Robot (5) SPFA(Shortest Path Faster Algorithm) (5) Shuffle (5) Sieve of Eratosthenes (5) Strongly Connected Components (5) Subarray Sum (5) Sudoku (5) Suffix Tree (5) Swap (5) Threaded (5) Tree - Creation (5) Warshall Floyd (5) Word Search (5) jiuzhang (5)

    Popular Posts