Skip to content

Commit 227a2a2

Browse files
committed
Update container with most water problem
1 parent 921efb7 commit 227a2a2

File tree

7 files changed

+127
-40
lines changed

7 files changed

+127
-40
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ List of Programs related to data structures and algorithms
6969
12. Search in rotated sorted array: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/12.searchRotatedSortedArray/searchRotatedSortedArray.js)
7070
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/12.searchRotatedSortedArray/searchRotatedSortedArray.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/11.searchRotatedSortedArray/searchRotatedSortedArray.md)
7171

72-
13. Container with most water: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/containerWithMostWater.js)
72+
13. Container with most water: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/13.containerWithMostWater/containerWithMostWater.js)
73+
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/13.containerWithMostWater/containerWithMostWater.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/13.containerWithMostWater/containerWithMostWater.md)
7374

7475
14. First missing positive number: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/firstMissingPositive/firstMissingPositive.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/firstMissingPositive/firstMissingPositive.md)
7576

src/java1/algorithms/array/ContainerWithMostWater.java

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package containerWithMostWater;
2+
public class ContainerWithMostWater {
3+
private static int mostWater(int[] heights) {
4+
int maxCapacity = 0;
5+
int left = 0, right = heights.length-1;
6+
while(left < right) {
7+
int currentCapacity = (right - left) * Math.min(heights[left], heights[right]);
8+
maxCapacity = Math.max(maxCapacity, currentCapacity );
9+
if(heights[left] < heights[right]) {
10+
left++;
11+
} else {
12+
right--;
13+
}
14+
}
15+
return maxCapacity;
16+
}
17+
18+
public static void main(String[] args) {
19+
int[] waterHeights1 = {3,9,4,1,5,4,7,1,7};
20+
System.out.println(mostWater(waterHeights1));
21+
22+
int[] waterHeights2 = {1,1};
23+
System.out.println(mostWater(waterHeights2));
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**Description:**
2+
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])`. You have to 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.
3+
4+
### Examples
5+
Example 1:
6+
Input: height = [3,9,4,1,5,4,7,1,7]
7+
Output: 49
8+
9+
Example 2:
10+
Input: height = [1,1]
11+
Output: 1
12+
13+
**Algorithmic Steps:**
14+
This problem is solved with the help of two pointer technique on opposite ends. The algorithmic approach can be summarized as follows:
15+
16+
1. Initialize maximum capacity of the container(i.e, `maxCapacity`) to zero.
17+
18+
2. Initialize two pointers(i.e, `left` and `right`) to represent both ends of an array.
19+
20+
3. Iterate an input array until left is less than right pointer.
21+
22+
4. Calculate the current container capacity(i.e, `area`) by the multiplication of width(difference between two indexes) and height(minimum height of two container heights).
23+
24+
**Note:** The minimum height is considered because water more than minimum of container heights spill the water.
25+
26+
5. Find the maximum capacity of the container by taking the maximum between maximum capacity so far and the current area.
27+
28+
6. If the left side height is less than right side height, increment the left pointer to result in maximum capacity.
29+
30+
7. If the right side height is less than left side height, decrement the right pointer to result in maximum capacity.
31+
32+
8. Repeat steps 4-7 until all the elements traversed.
33+
34+
9. Return the maximum capcity of the container.
35+
36+
**Time and Space complexity:**
37+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because the left or right pointer moved towards the other until they meet in each iteration, the elements are traversed once.
38+
39+
Here, we don't use any additional datastructure other than two left pointer variables. Hence, the space complexity will be O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function mostWater(heights) {
2+
let maxCapacity = 0;
3+
let left = 0, right = heights.length-1;
4+
5+
while(left < right) {
6+
let currentCapacity = (right - left) * Math.min(heights[left], heights[right]);
7+
maxCapacity = Math.max(maxCapacity, currentCapacity);
8+
9+
if(heights[left] < heights[right]) {
10+
left++;
11+
} else {
12+
right--;
13+
}
14+
}
15+
return maxCapacity;
16+
}
17+
18+
let waterHeights1 = [3,9,4,1,5,4,7,1,7];
19+
console.log(mostWater(waterHeights1));
20+
21+
let waterHeights2 = [1, 1];
22+
console.log(mostWater(waterHeights2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**Description:**
2+
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])`. You have to 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.
3+
4+
### Examples
5+
Example 1:
6+
Input: height = [3,9,4,1,5,4,7,1,7]
7+
Output: 49
8+
9+
Example 2:
10+
Input: height = [1,1]
11+
Output: 1
12+
13+
**Algorithmic Steps:**
14+
This problem is solved with the help of two pointer technique on opposite ends. The algorithmic approach can be summarized as follows:
15+
16+
1. Initialize maximum capacity of the container(i.e, `maxCapacity`) to zero.
17+
18+
2. Initialize two pointers(i.e, `left` and `right`) to represent both ends of an array.
19+
20+
3. Iterate an input array until left is less than right pointer.
21+
22+
4. Calculate the current container capacity(i.e, `area`) by the multiplication of width(difference between two indexes) and height(minimum height of two container heights).
23+
24+
**Note:** The minimum height is considered because water more than minimum of container heights spill the water.
25+
26+
5. Find the maximum capacity of the container by taking the maximum between maximum capacity so far and the current area.
27+
28+
6. If the left side height is less than right side height, increment the left pointer to result in maximum capacity.
29+
30+
7. If the right side height is less than left side height, decrement the right pointer to result in maximum capacity.
31+
32+
8. Repeat steps 4-7 until all the elements traversed.
33+
34+
9. Return the maximum capcity of the container.
35+
36+
**Time and Space complexity:**
37+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because the left or right pointer moved towards the other until they meet in each iteration, the elements are traversed once.
38+
39+
Here, we don't use any additional datastructure other than two left pointer variables. Hence, the space complexity will be O(1).

src/javascript/algorithms/array/containerWithMostWater.js

-18
This file was deleted.

0 commit comments

Comments
 (0)