Simple and Easy to Understand with Explanation
Video Explanation\nhttps://youtu.be/tmZ_Ev1kZN8\n\nThere are two ways to solve this:\n1. using arrays to store altitudes then finding max altitude in that array
Video Explanation\nhttps://youtu.be/tmZ_Ev1kZN8\n\nThere are two ways to solve this:\n1. using arrays to store altitudes then finding max altitude in that array\n2. store max and current altitude to a variable, then compare current altitude to max altitude. If current altitude is greater simply overwrite max altitude\n\n\n/**\n * 1. store altitudes in an array\n * 2. find max altitute\n * @param {number[]} gain     array of net gain/loss in altitude between two points\n * @returns {number}          maximum altitude\n * //* Time: O(n)\n * Space: O(n)\n */\n\nRuntime: 80 ms, faster than 61.27% of JavaScript online submissions for Find the Highest Altitude.\nMemory Usage: 38.7 MB, less than 39.62% of JavaScript online submissions for Find the Highest Altitude.\n\n```\n\nconst largestAltitude1 = gain => {\n\tconst altitudes = [0];\n\n\tfor (let i = 0; i < gain.length; i++) {\n\t\taltitudes.push(altitudes[i] + gain[i]);\n\t}\n\n\treturn Math.max(...altitudes);\n};\n\n```\n\nRuntime: 80 ms, faster than 61.27% of JavaScript online submissions for Find the Highest Altitude.\nMemory Usage: 38.7 MB, less than 39.62% of JavaScript online submissions for Find the Highest Altitude.\n\n\n```\nconst largestAltitude2 = gain => {\n\treturn Math.max(\n\t\t...gain.reduce((acc, cv, i) => (acc.push(cv + acc[i]), acc), [0])\n\t);\n};\n```\n\n\n/**\n * 1. store current altitude and maximum altitude\n * 2. compare current altitude and maximum altitude and if current altitude is greater, overwrite max\n * @param {number[]} gain     array of net gain/loss in altitude between two points\n * @returns {number}          maximum altitude\n * //* Time: O(n)\n * //* Space: O(1)\n */\n\nRuntime: 76 ms, faster than 80.64% of JavaScript online submissions for Find the Highest Altitude.\nMemory Usage: 38.4 MB, less than 70.57% of JavaScript online submissions for Find the Highest Altitude.\n\n\n```\nconst largestAltitude3 = gain => {\n\tlet [max, currentAltitude] = [0, 0];\n\n\tfor (let i = 0; i < gain.length; i++) {\n\t\tcurrentAltitude = gain[i] + currentAltitude;\n\t\tif (currentAltitude > max) max = currentAltitude;\n\t}\n\n\treturn max;\n};\n```\n
['JavaScript']