Very Easy Image Explanation Prefix sum type approach||C++,Java
Intuition and Approach\nIn the given question we have the difference between two heights  which is i->i+1 and hence they are depending on their original height 
# Intuition and Approach\nIn the given question we have the difference between two heights  which is `i->i+1` and hence they are depending on their original height and not on the net height which is given,\n****solving first test case given in question***\n![WhatsApp Image 2023-06-19 at 9.07.46 AM.jpeg](https://assets.leetcode.com/users/images/d92db95f-19de-458f-a6ad-68cfb1d1831c_1687145988.8115046.jpeg)\nSo as to find the original height values we will do the following and understand what is going on behind the question,\n*(g represent gain)*\n![WhatsApp Image 2023-06-19 at 9.07.45 AM.jpeg](https://assets.leetcode.com/users/images/7440d5dd-b3f8-49e9-9300-8a63d8ad72c2_1687146009.4597692.jpeg)\nConsidering above you can get to know that we can easily compute the altitude values one by one,\n```\ni.e. altitude[0]=0 ;given in question\n     g[1]-0=-5 -> altitude[1]=g[1]=-5+0=-5\n     g[2]-(-5)=1 -> altitude[2]=g[2]=1-5=-4\n     g[3]-(-4)=5 -> altitude[3]=g[3]=5-4=1\n     g[4]-(1)=0 -> altitude[4]=g[4]=0-1=-1\n```\n![WhatsApp Image 2023-06-19 at 9.07.45 AM (1).jpeg](https://assets.leetcode.com/users/images/ae617bd8-db5c-472a-9c88-1f74bb4bd98f_1687145993.6847088.jpeg)\nSo, we have to find maximum of all altitudes which we can do by using inbuilt function *max_element\n\n**(Get the Question? Upvote:comment)**\n# Complexity\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)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int largestAltitude(vector<int>& gain) {\n        int ans=INT_MIN;\n        vector<int> altitude;\n\n        //pushing the starting altiude value\n        altitude.push_back(0);\n\n        for(int i=1;i<=gain.size();i++){\n            altitude.push_back(gain[i-1]+altitude[i-1]);\n        }\n\n        //check the altitude vector by uncommenting below\n        // for(auto val:altitude){\n        //     cout<<val <<" ";\n        // }\n\n        ans=*max_element(altitude.begin(),altitude.end());\n\n        return ans;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int largestAltitude(int[] gain) {\n        int ans = Integer.MIN_VALUE;\n        List<Integer> altitude = new ArrayList<>();\n\n        // Pushing the starting altitude value\n        altitude.add(0);\n\n        for (int i = 1; i <= gain.length; i++) {\n            altitude.add(gain[i - 1] + altitude.get(i - 1));\n        }\n\n        ans = Collections.max(altitude);\n\n        return ans;\n    }\n}\n\n```\n```python []\nfrom typing import List\n\nclass Solution:\n    def largestAltitude(self, gain: List[int]) -> int:\n        ans = float(\'-inf\')\n        altitude = [0]\n\n        # Pushing the starting altitude value\n        altitude.append(0)\n\n        for i in range(1, len(gain) + 1):\n            altitude.append(gain[i - 1] + altitude[i - 1])        \n\n        ans = max(altitude)\n\n        return ans\n\n```\n\n
['Array', 'Math', 'Prefix Sum', 'C++']