-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainerWater.java
More file actions
36 lines (32 loc) · 1.03 KB
/
containerWater.java
File metadata and controls
36 lines (32 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
class containerWater {
public static int maxArea(int[] height) {
int maxVol = 0; // keep track of the max Volume
int left = 0; // left pointer
int right = height.length - 1; // right pointer
while (right > left) {
// tallness of container is min of 2 walls
int tall = Math.min(height[left], height[right]);
int width = right - left;
// volume
int vol = tall * width;
maxVol = Math.max(vol, maxVol);
// move in the pointer of the shorter side
if (height[left] < height[right]) {
left++;
} else right--;
}
return maxVol;
}
public static void main(String[] args) {
int[] height = {1,8,6,2,5,4,8,3,7};
System.out.println(maxArea(height));
}
}
/*
* Create a var to return final number
* 2 pointers at each end of array
* maxArea = height * length
* find smaller side/height; min of 2 heights
* length = length of array - 1
*/