largestAltitude solution with easy approach
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
# 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<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nInitialize 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.\nIterate over the gain vector, starting from the first element.\nAt each step, update the currentAltitude by adding the gain of the current step.\nCompare the currentAltitude with the maxAltitude. If it is greater, update maxAltitude to the new value.\nRepeat steps 3-4 for all elements in the gain vector.\nOnce the iteration is complete, maxAltitude will hold the highest altitude reached.\nReturn 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<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n**For python code**\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n**For java code**\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nWe can do both either in same way but i used different approaches.\n\n# Code\n```python []\nclass Solution:\n    def largestAltitude(self, gain: List[int]) -> int:\n        max_alt = 0\n        curr = 0\n        for g in gain:\n            curr += g\n            max_alt = max(max_alt,curr)\n        return max_alt\n        \n    \n```\n```java []\nclass Solution {\n    public int largestAltitude(int[] gain) {\n        int[] brr = new int[gain.length + 1];\n        brr[0] = 0;\n        brr[1]= brr[0] + gain[0];\n        for(int i =2 ; i< brr.length ; i++){\n            brr[i] = brr[i-1] + gain[i-1];\n        }\n        int max = Integer.MIN_VALUE;\n        for(int  i =0 ; i< brr.length;i++){\n            max = Math.max(brr[i],max);\n        }\n        return max;\n        \n    }\n}\n```
['Array', 'Prefix Sum', 'Java', 'Python3']