-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCointainsWithMostWater(Array)
More file actions
30 lines (28 loc) · 992 Bytes
/
Copy pathCointainsWithMostWater(Array)
File metadata and controls
30 lines (28 loc) · 992 Bytes
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
// This function is with time complexity of O(n^2) which use 2 for loops
public static int containsMaxWater(int[] a) {
int maxArea = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++ ) {
int distance = j - i;
int height = Math.min(a[i], a[j]);
int area = height * distance;
maxArea = Math.max(area, maxArea);
}
}
return maxArea;
}
// This function is with time complexity of O(n) which use 2 pointer approach
public static int containsWithMaxWater(int[] a) {
int l = 0, r = a.length - 1;
int maxArea = 0;
while (l < r) {
int height = Math.min(a[l],a[r]);
int distance = r - l;
int area = distance * height;
maxArea = Math.max(area, maxArea);
if (a[l] < a[r])
l++;
else r--;
}
return maxArea;
}