Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8270e9a

Browse files
committed
Create Lt11.java
1 parent e13afb9 commit 8270e9a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎LeetCode/Blind75/Lt11.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package Blind75;
2+
/*
3+
* You are 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]).
4+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
5+
Return the maximum amount of water a container can store.
6+
Notice that you may not slant the container.
7+
*/
8+
public class Lt11 {
9+
public static void main(String[] args) {
10+
int[] height= {1,1};
11+
12+
System.out.println(maxArea(height));
13+
}
14+
15+
public static int maxArea(int[] height) {
16+
int left = 0;
17+
int right = height.length - 1;
18+
int max = 0;
19+
while(left < right){
20+
int w = right - left; //width for every iteration
21+
int h = Math.min(height[left], height[right]); //height is the min between left and right index
22+
int area = h * w; //area is height * width
23+
max = Math.max(max, area); //max between the current max area vs current area of the iteration
24+
if(height[left] < height[right]) left++;
25+
else if(height[left] > height[right]) right--;
26+
else {
27+
left++;
28+
right--;
29+
}
30+
}
31+
return max;
32+
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /