Easy JAVA Code || Beginner Friendly || Beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nThe given code represents a Java class called Solution with a method largestAltitude. T
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe given code represents a Java class called Solution with a method largestAltitude. The method takes an array gain as input, which represents the altitude gains at various points. The goal is to find the highest altitude reached during the journey.\n\nHere\'s how the code works:\n\nIt initializes two variables max and temp to 0.\n\nmax will keep track of the maximum altitude reached so far.\ntemp will store the current altitude as the loop progresses.\nIt then loops through the gain array from the beginning to the end.\n\nAt each iteration, it adds the current gain gain[i] to the temp variable, representing the altitude at that point.\nIt updates the max variable by taking the maximum value between the current max and temp using the Math.max method.\nThis ensures that max always holds the highest altitude reached during the journey up to the current point.\nFinally, the method returns the maximum altitude stored in the max variable.\n\nIn summary, the largestAltitude method calculates the altitude gained at each point and keeps track of the highest altitude reached during the journey. The maximum altitude is then returned as the result.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach used in the given code is a simple iterative approach to find the largest altitude reached during a journey. Here\'s a step-by-step explanation of the approach:\n\nInitialize two variables, max and temp, to 0. These variables will be used to track the maximum altitude reached and the current altitude, respectively.\n\nIterate through the gain array using a for loop. At each iteration:\n\nAdd the current gain gain[i] to the temp variable. This represents the altitude at the current point.\nUpdate the max variable by taking the maximum value between the current max and temp using the Math.max() method. This ensures that max always holds the highest altitude reached during the journey up to the current point.\nAfter iterating through the entire gain array, the max variable will hold the largest altitude reached during the journey.\n\nReturn the value stored in the max variable as the result.\n\nIn summary, the approach calculates the altitude at each point by adding the gains from the gain array. It keeps track of the maximum altitude reached so far and returns that value as the final result.\n\n# Complexity\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\n# Code\n```\nclass Solution {\n    public int largestAltitude(int[] gain) {\n        int max=0,temp=0;\n        for(int i=0;i<gain.length;i++){\n            temp+=gain[i];\n            max=Math.max(max,temp);\n        }\n        return max;\n    }\n}\n```
['Java']