-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathContainer.js
More file actions
65 lines (33 loc) · 1.12 KB
/
Copy pathContainer.js
File metadata and controls
65 lines (33 loc) · 1.12 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 1. Container With Most Water
// Problem: You are given an integer array height where height[i] represents the height of a vertical line at position i. Find two lines that together with the x-axis form a container, such that the container contains the most water.
function maxArea(height) {
let left = 0, right = height.length - 1;
let maxArea = 0;
while (left < right) {
const minHeight = Math.min(height[left], height[right]);
maxArea = Math.max(maxArea, minHeight * (right - left));
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
function maxArea(height){
let left = 0;
let right = height.length - 1;
let maxArea = 0;
while(left < right){
const width = right - left;
const currentHeight = Math.min(height[left], height[right]);
const currentArea = width * currentHeight;
maxArea = Math.max(maxArea,currentArea);
if(height[left] < height[right]){
left++;
}else{
right--
}
}
return maxArea;
}