Skip to content

Commit 83a0bbd

Browse files
committed
Refine 011_Container_With_Most_Water
1 parent 3564604 commit 83a0bbd

5 files changed

+31
-63
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ __pycache__/
33
*.py[cod]
44
*$py.class
55
.idea/
6-
6+
*.iml
77
# C extensions
88
*.so
99

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
2020
| 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/007_Reverse_Integer.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/007_Reverse_Integer.java) | Overflow when the result is greater than 2147483647 or less than -2147483648.
2121
| 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/008_String_to_Integer(atoi).py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/008_String_to_Integer(atoi).java) | Overflow, Space, and negative number |
2222
| 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/009_Palindrome_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/009_Palindrome_Number.java) | Get the len and check left and right with 10^len, 10 |
23+
| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/011_Container_With_Most_Water.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/011_Container_With_Most_Water.java) | 1. Brute Force, O(n^2) and O(1)<br>2. Two points, O(n) and O(1) |
2324
| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/012_Integer_to_Roman.py) | [Background knowledge](http://www.rapidtables.com/convert/number/how-number-to-roman-numerals.htm) Just like 10-digit number, divide and minus |
2425
| 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/013_Roman_to_Integer.py) | Add all curr, if curr > prev, then need to subtract 2 * prev |
2526
| 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/015_3Sum.py) | 1. Sort and O(n^2) search with three points <br>2. Multiple Two Sum (Problem 1) |
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
4+
int maxArea = 0;
5+
int left = 0;
6+
int right = height.length - 1;
7+
8+
while (left < right) {
9+
maxArea = Math.max(maxArea, (right - left) * Math.min(height[left], height[right]));
10+
// Two points
11+
if (height[left] < height[right]) left++;
12+
else right--;
13+
}
14+
return maxArea;
15+
}
16+
}

java/011_containerwithmostwater.java

-20
This file was deleted.
+13-42
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,14 @@
1-
# class Solution(object):
2-
# def maxArea(self, height):
3-
# """
4-
# :type height: List[int]
5-
# :rtype: int
6-
# """
7-
class Solution(object):
8-
# def maxArea(self, height):
9-
# # brute force
10-
# ls = len(height)
11-
# left, right = 0, ls - 1
12-
# result = 0
13-
# while left < right:
14-
# result = max(min(height[left], height[right]) * (right - left), result)
15-
# if height[left] > height[right]:
16-
# right -= 1
17-
# else:
18-
# left += 1
19-
# return result
20-
21-
def maxArea(self, height):
22-
# skip some choices
23-
ls = len(height)
24-
lm = min(height[0], height[ls - 1])
25-
max_v = lm * (ls - 1)
26-
low = 0
27-
high = ls - 1
28-
while low < high:
29-
while height[low] < lm and low < ls:
30-
low += 1
31-
while height[high] < lm and high < ls:
32-
high -= 1
33-
if low > high:
34-
break
35-
m = min(height[low], height[high])
36-
if m * (high - low) > max_v:
37-
max_v = m * (high - low)
38-
lm = m
39-
if height[low] < height[high]:
40-
low += 1
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
# Two points
4+
left, right = 0, len(height) - 1
5+
result = 0
6+
while left < right:
7+
result = max(min(height[left], height[right]) * (right - left), result)
8+
if height[left] > height[right]:
9+
# remove right
10+
right -= 1
4111
else:
42-
high -= 1
43-
return max_v
12+
# remove left
13+
left += 1
14+
return result

0 commit comments

Comments
 (0)