
Problem
Solution
Submissions
Container With Most Water
Certification: Intermediate Level
Accuracy: 0%
Submissions: 1
Points: 10
Write a C program to find the container with the most water. Given n non-negative integers representing the heights of n vertical lines (or walls) placed at position 1, 2, ..., n, find two lines that, together with the x-axis, form a container that holds the most water.
Example 1
- Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
- Output: 49
- Explanation:
- The container is formed by the two lines at positions 2 and 9.
- Height of the left line is 8, height of the right line is 7.
- The width of the container is 9 - 2 = 7.
- The area of water the container can hold is min(8, 7) * 7 = 7 * 7 = 49.
- The container is formed by the two lines at positions 2 and 9.
Example 2
- Input: height = [1, 1]
- Output: 1
- Explanation:
- The container is formed by the two lines at positions 1 and 2.
- Height of both lines is 1. The width of the container is 2 - 1 = 1.
- The area of water the container can hold is min(1, 1) * 1 = 1 * 1 = 1
- The container is formed by the two lines at positions 1 and 2.
Constraints
- n == height.length
- 2 <= n <= 10^5
- 0 <= height[i] <= 10^4
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use the two-pointer technique with one pointer at the beginning and one at the end
- Calculate the area between the two pointers
- Move the pointer pointing to the shorter line inward
- Keep track of the maximum area seen so far
- Continue until the two pointers meet