✅Beats 100% || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥
Intuition:\nTo find the highest altitude, we need to calculate the cumulative sum of the altitude gains at each step. Starting from the initial altitude of 0, w
# Intuition:\nTo find the highest altitude, we need to calculate the cumulative sum of the altitude gains at each step. Starting from the initial altitude of 0, we keep adding the gain at each step to the current altitude. The highest altitude reached during this process is the answer we are looking for.\n\n# Approach:\n1. Initialize two variables: `maxAltitude` and `currentAltitude` to keep track of the maximum altitude encountered so far and the current altitude, respectively. Set both variables to 0.\n2. Iterate over the `gain` vector, starting from the first element.\n3. At each step, update the `currentAltitude` by adding the gain of the current step.\n4. Compare the `currentAltitude` with the `maxAltitude`. If it is greater, update `maxAltitude` to the new value.\n5. Repeat steps 3-4 for all elements in the `gain` vector.\n6. Once the iteration is complete, `maxAltitude` will hold the highest altitude reached.\n7. Return the value of `maxAltitude` as the result.\n\nThis approach ensures that we keep track of the maximum altitude encountered during the traversal and avoids unnecessary modifications to the input vector. By iterating over the `gain` vector only once, the solution is efficient and provides the correct result.\n# Code\n\n```C++ []\nclass Solution {\npublic:\n    int largestAltitude(vector<int>& gain) {\n        int maxAltitude = 0;\n        int currentAltitude = 0;\n        \n        for (int i = 0; i < gain.size(); i++) {\n            currentAltitude += gain[i];\n            maxAltitude = max(maxAltitude, currentAltitude);\n        }\n        \n        return maxAltitude;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int largestAltitude(int[] gain) {\n        int maxAltitude = 0;\n        int currentAltitude = 0;\n        \n        for (int i = 0; i < gain.length; i++) {\n            currentAltitude += gain[i];\n            maxAltitude = Math.max(maxAltitude, currentAltitude);\n        }\n        \n        return maxAltitude;\n    }\n}\n```\n```Python3 []\nclass Solution:\n    def largestAltitude(self, gain: List[int]) -> int:\n        maxAltitude = 0\n        currentAltitude = 0\n        \n        for g in gain:\n            currentAltitude += g\n            maxAltitude = max(maxAltitude, currentAltitude)\n        \n        return maxAltitude\n```\n\n![CUTE_CAT.png](https://assets.leetcode.com/users/images/9d32a68c-a698-414f-b82f-e7ecc693f3eb_1687147131.7430437.png)\n\n\n**If you found my solution helpful, I would greatly appreciate your upvote, as it would motivate me to continue sharing more solutions.**\n\n
['Array', 'Prefix Sum', 'C++', 'Java', 'Python3']