Blind 75
Container With Most Water
- Problem
- LC 11
- Category
- Array
- File
- blind75_LC11ContainerWithMostWater.java
- Path
- pkg5leetcode/blind75/blind75_LC11ContainerWithMostWater.java
- Package
- pkg5leetcode.blind75
- Command
- java pkg5leetcode/blind75/blind75_LC11ContainerWithMostWater.java
- Approach
- Two pointers move shorter line inward.
- Complexity
- Time O(n), Space O(1)
There is no in-browser runner. This is the file from the curriculum, unchanged.
1package pkg5leetcode.blind75;2 3/*4 * Container With Most Water | LC 115 * APPROACH: Two pointers move shorter line inward.6 * COMPLEXITY: Time O(n), Space O(1)7 */8public class blind75_LC11ContainerWithMostWater {9 static int maxArea(int[] h) {10 int lo = 0, hi = h.length - 1, best = 0;11 while (lo < hi) {12 best = Math.max(best, Math.min(h[lo], h[hi]) * (hi - lo));13 if (h[lo] < h[hi]) lo++; else hi--;14 }15 return best;16 }17 18 public static void main(String[] args) {19 check(maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}) == 49, "case1");20 check(maxArea(new int[]{1, 1}) == 1, "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}