JavaScript O(n) solution, line sweep variant
I didn\'t remember / didn\'t come up with the line sweep algorithm while solving this problem, but managed to get some variant of it.\n\nThe basic idea is to ma
I didn\'t remember / didn\'t come up with the line sweep algorithm while solving this problem, but managed to get some variant of it.\n\nThe basic idea is to maintain 2 arrays:\n- no of people born in a given year;\n- no of people dead in a given year;\n\nThen, we can go over years 1950 - 2050 and calculate no of people still alive by adding ones that were born and substracting ones that were dead.\n\nHere is the complete code:\n\n```\nvar maximumPopulation = function(logs) {\n    const s = new Uint8Array(101);\n    const e = new Uint8Array(101);\n    \n    for (const [rs, re] of logs) {\n        s[rs - 1950]++;\n        e[re - 1950]++;\n    }\n    \n    let noOfPeople = 0, maxNoOfPeople = 0, maxYear = undefined;\n    for (let i = 0; i <= 100; i++) {\n        noOfPeople += s[i];\n        noOfPeople -= e[i];\n        \n        if (noOfPeople > maxNoOfPeople) {\n            maxNoOfPeople = noOfPeople;\n            maxYear = i;\n        }\n    }\n    \n    return maxYear + 1950;\n};\n```\n\nImplementation note: I\'m using JS typed arrays of size 101 to limit memory usage (additional bonus: those arrays are intitialised with 0 by default).
['JavaScript']