LeetCode 75
Find the Highest Altitude
- Problem
- LC 1732
- Topic
- Prefix Sum
- File
- official75_LC1732FindTheHighestAltitude.java
- Path
- pkg5leetcode/official75/official75_LC1732FindTheHighestAltitude.java
- Package
- pkg5leetcode.official75
- Command
- java pkg5leetcode/official75/official75_LC1732FindTheHighestAltitude.java
- Approach
- Prefix sum track max altitude.
- Complexity
- Time O(n), Space O(1)
There is no in-browser runner. This is the file from the curriculum, unchanged.
1package pkg5leetcode.official75;2 3/*4 * Find the Highest Altitude | LC 17325 * APPROACH: Prefix sum track max altitude.6 * COMPLEXITY: Time O(n), Space O(1)7 */8public class official75_LC1732FindTheHighestAltitude {9 static int largestAltitude(int[] gain) {10 int alt = 0, best = 0;11 for (int g : gain) {12 alt += g;13 best = Math.max(best, alt);14 }15 return best;16 }17 18 public static void main(String[] args) {19 check(largestAltitude(new int[]{-5,1,5,0,-7}) == 1, "case1");20 check(largestAltitude(new int[]{-4,-3,-2,-1}) == 0, "case2");21 System.out.println("all tests passed");22 }23 24 static void check(boolean cond, String name) {25 if (!cond) throw new AssertionError("FAILED: " + name);26 System.out.println(" PASS " + name);27 }28}