Easy level -100% beats in [Java][C++][Python3] -Tc: O(n) Sc: O(1) || Array || Sum||Detailed Approach
Intuition\n Describe your first thoughts on how to solve this problem. \nwe aims to find the largest altitude reached given a series of altitude gains. It calcu
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe aims to find the largest altitude reached given a series of altitude gains. It calculates the cumulative altitude by adding the gains at each step and keeps track of the maximum cumulative altitude reached.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIt iterates through the gain vector, representing the altitude gains. It initializes two variables, maxc and cal, to keep track of the maximum cumulative altitude and the current cumulative altitude, respectively.\n\nIn each iteration, it adds the current gain gain[i] to cal. Then, it updates maxc by taking the maximum of the current maxc and cal. This ensures that maxc always holds the highest altitude reached so far.\n\nAfter iterating through all the gains, it returns maxc, which represents the largest altitude reached.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of this code is O(n), where n is the number of elements in the gain vector. The code iterates through each gain once, performing constant-time operations for each iteration.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of this code is O(1) since it only uses a constant amount of additional space for the variables n, maxc, and cal. The space required does not depend on the size of the input.\n\n\n\n# Code\n```Java []\nclass Solution {\n    public int largestAltitude(int[] gain) {\n        int n = gain.length;\n        int maxc = 0, cal = 0;\n        for (int i = 0; i < n; i++) {\n            cal += gain[i];\n            maxc = Math.max(maxc, cal);\n        }\n        return maxc;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int largestAltitude(vector<int>& gain) {\n        int n = gain.size();\n        int maxc=0, cal=0;\n        \n        for(int i =0; i<n; i++){\n           cal += gain[i]; \n           maxc = max( maxc, cal);\n           \n        }\n       return maxc;\n\n    }\n};\n```\n```Python []\nclass Solution:\n    def largestAltitude(self, gain: List[int]) -> int:\n        n = len(gain)\n        maxc = 0\n        cal = 0\n        for i in range(n):\n            cal += gain[i]\n            maxc = max(maxc, cal)\n        return maxc\n```
['Array', 'Prefix Sum', 'C++', 'Java', 'Python3']