Simple Line Sweep Algorithm
More Line Sweep Problems:\nhttps://leetcode.com/problem-list/a0l633d6/\nhttps://leetcode.com/problem-list/a06u4275/\n\nhttps://carefree-ladka.github.io/js.enigm
More **Line Sweep** Problems:\nhttps://leetcode.com/problem-list/a0l633d6/\nhttps://leetcode.com/problem-list/a06u4275/\n\nhttps://carefree-ladka.github.io/js.enigma/docs/tutorial-basics/LineSweep\n\n**Intuition:**\n - Take a list with start and end points \n - [start, 1, value] & [end, -1, val] Here 1 means starting an interval and -1 interval end.\n - After pushing events to points list sort it by start time if equal sort by end\n - Now Check type is 1 (starting interval), keep adding val and store in ans\n - If type -1 , it means we have reached an end of an interval, calculate max \n - Return maxTotal \n\n\n---\n\n```javascript []\n/**\n * @param {number[][]} events\n * @return {number}\n */\nvar maxTwoEvents = function (events) {\n  //const points = [];\n  //events.forEach(([s, e, v]) => points.push([s, 1, v], [e, -1, v]));\n  const points = events.flatMap(([s, e, v]) => [[s, 1, v], [e, -1, v]]);\n  \n  points.sort((a, b) => a[0] - b[0] || b[1] - a[1]);\n\n  let maxDone = 0, maxTotal = 0;\n  for (const [_, type, val] of points) {\n    if (type === 1) maxTotal = Math.max(maxTotal, val + maxDone);\n    else maxDone = Math.max(maxDone, val);\n  }\n\n  return maxTotal\n};\n```
['Array', 'Line Sweep', 'Sorting', 'JavaScript']