Back to Browse

20 Container with Most Water | LeetCode | Java Solution in Hindi

43 views
Apr 30, 2024
27:41

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container. Example 1: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. Example 2: Input: height = [1,1] Output: 1 Problem Explanation The problem presents a scenario where n vertical lines are positioned at intervals along the x-axis. Each line has a height given by an array element at a corresponding index. The challenge is to select two lines that, together with the x-axis, will form a container holding the maximum amount of water. This container's capacity is determined by the shorter of the two lines (since water would overflow over the shorter line) and the horizontal distance between them. The essence of the problem is to maximize the product of the height of the shorter line and the distance between the lines. Intuition and Approach The straightforward approach would be to calculate the area for every possible pair of lines and keep track of the maximum. This brute force method, however, would be inefficient for large arrays, as it requires O(n²) time due to nested loops checking every pair. A more efficient approach uses a two-pointer technique, leveraging the following intuitions: Start with Maximum Width: The initial potential for holding water is highest when the container spans the maximum width, which is between the first and the last line. Hence, we start with two pointers: one at the beginning (left) and one at the end (right) of the array. Move Towards Higher Height: The capacity is limited by the shorter line. To potentially find a container with more water, after calculating the water contained between the lines at left and right, move the pointer pointing to the shorter line inward. This is because moving the shorter line could lead to a taller line, potentially increasing the area. If the two lines are of equal height, you can choose to move either pointer, but typically, moving both towards the center can be effective. Calculate and Update Maximum Area: For each pair of lines pointed to by left and right, calculate the area formed by these lines and update the maximum area encountered so far. Repeat Until the Pointers Meet: Continue this process until the left pointer meets or surpasses the right pointer. At this point, every potential container has been evaluated.

Download

1 formats

Video Formats

360pmp439.9 MB

Right-click 'Download' and select 'Save Link As' if the file opens in a new tab.

20 Container with Most Water | LeetCode | Java Solution in Hindi | NatokHD