Container With Most Water Problem Solved in Java

🔥 Day 95 of my 100 Days of Code Problem: Container With Most Water (LeetCode #11) Problem Description: You are given an integer array height of length n. Each element represents a vertical line on the x-axis. Find two lines that together with the x-axis form a container that holds the most water. Return the maximum amount of water that can be contained. Java Solution: class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; int maxWater = 0; while (left < right) { int h = Math.min(height[left], height[right]); int w = right - left; maxWater = Math.max(maxWater, h * w); // Move the pointer pointing to the shorter line if (height[left] < height[right]) { left++; } else { right--; } } return maxWater; } } Time Complexity: O(n) Space Complexity: O(1) #Day95 #LeetCode #100DaysOfCode #Java #TwoPointers #Array

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories