🎒 "Climb to the Top! 🏔️ Calculating the Highest Altitude Step-by-Step ⬆️⬇️"
Problem Intuition \uD83E\uDDED:\n\nImagine you\'re hiking \uD83E\uDD7E on a mountain \uD83C\uDF04. You start at sea level (altitude 0) and every step you take e
# Problem Intuition \uD83E\uDDED:\n\nImagine you\'re hiking \uD83E\uDD7E on a mountain \uD83C\uDF04. You start at sea level (altitude 0) and every step you take either goes up \u2B06\uFE0F or down \u2B07\uFE0F, depending on the gain values in an array. Your goal is to figure out the highest altitude you reach during your hike! Sounds like fun, right? Let\'s dive into how to solve this step by step.\n\n# Approach\n1. Start at Sea Level (altitude = 0) \uD83C\uDF0A:\n    <ul><li>   Initially, you\'re at 0 altitude.</li>\n    <li>The first step might make you go up \u2B06\uFE0F or down \u2B07\uFE0F. We need to keep track of the highest point we reach. Let\'s call this maxi (short for maximum altitude).</li>\n</ul>\n\n2. Cumulative Altitude Calculation \uD83D\uDD22:\n    <ul><li>The values in gain[] don\u2019t tell you directly where you are but how much the altitude changes with each step.</li>\n    <li>For each step, we update our current altitude based on the previous step: gain[i] = gain[i] + gain[i-1]. This way, we\u2019re calculating the total altitude after each step.</li>\n</ul>\n\n3. Track the Highest Altitude \uD83C\uDFD4\uFE0F:\n    <ul><li>With each new altitude, we check if it\'s the highest we\'ve reached so far.</li>\n    <li>We do this by comparing the current altitude gain[i] to maxi. If gain[i] is bigger, we update maxi.</li>\n</ul>\n\n4. Return the Maximum Altitude \uD83C\uDFC5:\n    <ul><li>After checking all steps, maxi will have the highest altitude we\u2019ve reached during the hike! This is what we return.</li></ul>\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$ \n\n# Example Walkthrough (\uD83D\uDEB6 + \u26F0\uFE0F = fun!):\n\nLet\'s say we have the gain[] array: [-5, 1, 5, 0, -7].\n\nWe start at sea level:\n<ul><li>\n    \uD83C\uDF0A Altitude = 0\n</li>\n</ul>\nNow let\u2019s walk through each step:\n<ul>\n<li>\n    Step 1: -5\n    \u2B07\uFE0F You go down by 5. Altitude is now 0 + (-5) = -5 \u274C (not higher than 0).\n</li>\n<li>\n    Step 2: +1\n    \u2B06\uFE0F You go up by 1. Altitude is now -5 + 1 = -4 \u274C (still not higher than 0).\n</li>\n<li>\n    Step 3: +5\n    \u2B06\uFE0F You climb by 5. Altitude is now -4 + 5 = 1 \uD83C\uDFC5 (new highest point so far!).\n</li>\n<li>\n    Step 4: +0\n    \u2796 You stay the same. Altitude remains 1. No change \uD83D\uDED1.\n</li>\n<li>\n    Step 5: -7\n \u2B07\uFE0F You go down by 7. Altitude is now 1 + (-7) = -6 \u274C (lower than 1).\n</li>\n</ul>\nFinally, the highest altitude you reached was 1, so that\'s what we return! \uD83C\uDF89\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int largestAltitude(vector<int>& gain) {\n        int maxi=max(0,gain[0]);\n        for(int i=1;i<gain.size();i++)\n        {\n            gain[i]+=gain[i-1];\n            maxi=max(maxi,gain[i]);\n        }\n\n        return maxi;\n    }\n};\n```\n\n
['Math', 'Prefix Sum', 'C++']