Advertisement

Container With Most Water - LeetCode 11 Solution

Container With Most Water - Complete Solution Guide

Container With Most Water is LeetCode problem 11, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given an integer array height of length n . There are n vertical lines drawn such that the two endpoints of the i th 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 [

Detailed Explanation

The problem asks us to find the largest area of water that can be contained between two vertical lines, defined by their heights in the input array `height`, and the x-axis. The width of the container is the distance between the two lines, and the height is the minimum of the two line heights. The container cannot be slanted.

Solution Approach

The provided solution employs a two-pointer approach. Two pointers, `left` and `right`, are initialized to the beginning and end of the `height` array, respectively. The algorithm iteratively calculates the area formed by the lines at `left` and `right`, updates the maximum area seen so far, and moves either the `left` or `right` pointer based on which line is shorter. The pointer associated with the shorter line is moved towards the center in hopes of finding a taller line. This process continues until the two pointers meet.

Step-by-Step Algorithm

  1. Step 1: Initialize two pointers, `left` to 0 and `right` to `len(height) - 1`.
  2. Step 2: Initialize `max_area` to 0.
  3. Step 3: While `left < right`, repeat steps 4-7.
  4. Step 4: Calculate the width of the container as `width = right - left`.
  5. Step 5: Calculate the height of the container as `h = min(height[left], height[right])`.
  6. Step 6: Calculate the current area as `current_area = width * h`.
  7. Step 7: Update `max_area = max(max_area, current_area)`.
  8. Step 8: If `height[left] < height[right]`, increment `left`; otherwise, decrement `right`.
  9. Step 9: Return `max_area`.

Key Insights

  • Insight 1: The area of the container is determined by the shorter of the two heights and the distance between the lines.
  • Insight 2: A brute-force approach (checking every possible pair of lines) would be inefficient. A two-pointer approach is more efficient.
  • Insight 3: Moving the pointer associated with the shorter height has the potential to increase the height of the container, thereby increasing the area. Moving the pointer associated with the taller height will always decrease the potential area (either decreasing width or maintaining/decreasing height).

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Two Pointers, Greedy.

Companies

Asked at: Accenture, Accolite, Adobe, Amazon, Apple, Atlassian, Bloomberg, ByteDance, Citadel, Coveo, Deloitte, Flipkart, Goldman Sachs, HSBC, HashedIn, Infosys, Intel, J.P. Morgan, Lenskart, Mastercard, Meta, Microsoft, Miro, Myntra, Nutanix, Oracle, PayPal, Paytm, QBurst, RBC, SAP, Salesforce, Samsung, ServiceNow, Snowflake, Sprinklr, Tesla, TikTok, Uber, Visa, Walmart Labs, Wix, Yahoo, Yandex, Zoho, Zoom, Zopsmart, oyo, razorpay, tcs.