Sweep Line Algorithm with Explanation
This is a clasic sweep line algorithm.\n\n1. Get all births and deaths in sorted array/map;\n2. Now if its a birth, then increment and if its a death then decre
This is a clasic sweep line algorithm.\n\n1. Get all births and deaths in sorted array/map;\n2. Now if its a birth, then increment and if its a death then decrement;\n3. And keep count of the population while doing so. Result is the max population.\n\nAs a reference, here are few more problems that could be solved by sweep line:\nhttps://leetcode.com/problems/my-calendar-iii/submissions/\nhttps://leetcode.com/problems/employee-free-time/submissions/\nhttps://leetcode.com/problems/meeting-scheduler/submissions/\n\n\n```\npublic class Solution {\n    public int MaximumPopulation(int[][] logs) {\n        \n        var map    = new SortedDictionary<int, int>();\n        var res    = 0;\n        var maxPop = 0;\n        var sum    = 0;\n        \n        foreach(var log in logs) {\n            if(!map.ContainsKey(log[0]))    map.Add(log[0], 0);\n            map[log[0]]++;\n            if(!map.ContainsKey(log[1]))    map.Add(log[1], 0);\n            map[log[1]]--;\n        }\n\n        foreach(var key in map.Keys){\n            \n            sum += map[key];\n            \n            if(sum > maxPop) {\n                maxPop = sum;\n                res = key;\n            }\n        }\n        \n        return res;\n    }\n}\n```
[]