Skip to content

Commit 2eabf43

Browse files
author
zhangbo54
committed
11. Container With Most Water
1 parent ac0e42b commit 2eabf43

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/leetcode/_11_/Main.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package leetcode._11_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-01.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}));
10+
// Integer.parseInt("1" + Integer.MAX_VALUE);
11+
}
12+
13+
static
14+
class Solution {
15+
public int maxArea(int[] height) {
16+
int i = 0;
17+
int j = height.length - 1;
18+
int water = 0;
19+
while (i < j) {
20+
water = Math.max((j - i) * Math.min(height[i], height[j]), water);
21+
if (height[i] < height[j]) {
22+
i++;
23+
} else {
24+
j--;
25+
}
26+
}
27+
return water;
28+
}
29+
}
30+
}

src/leetcode/_11_/md.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### [11\. Container With Most Water](https://leetcode.com/problems/container-with-most-water/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given _n_ non-negative integers _a<sub style="display: inline;">1</sub>_, _a<sub style="display: inline;">2</sub>_, ..., _a<sub style="display: inline;">n </sub>_, where each represents a point at coordinate (_i_, _a<sub style="display: inline;">i</sub>_). _n_ vertical lines are drawn such that the two endpoints of line _i_ is at (_i_, _a<sub style="display: inline;">i</sub>_) and (_i_, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
7+
8+
**Note: **You may not slant the container and _n_ is at least 2.
9+
10+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
11+
12+
<small style="display: inline;">The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49\.</small>
13+
14+
**Example:**
15+
16+
```
17+
Input: [1,8,6,2,5,4,8,3,7]
18+
Output: 49```
19+
20+
21+
#### Solution
22+
23+
Language: **Java**
24+
25+
```java
26+
class Solution {
27+
   public int maxArea(int[] height) {
28+
       int i = 0;
29+
       int j = height.length - 1;
30+
       int water = 0;
31+
       while (i < j) {
32+
           water = Math.max((j - i) * Math.min(height[i], height[j]), water);
33+
           if (height[i] < height[j]) {
34+
               i++;
35+
          } else {
36+
               j--;
37+
          }
38+
      }
39+
       return water;
40+
  }
41+
}
42+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy