GeezerVison is showing github.com — Refine 011_Container_With_Most_Water · sunshinenny/leetcode-python-java@83a0bbd
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ __pycache__/ | 3 | 3 | *.py[cod] | 4 | 4 | *$py.class | 5 | 5 | .idea/ | 6 | | - | | 6 | +*.iml | 7 | 7 | # C extensions | 8 | 8 | *.so | 9 | 9 |
| |
Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal | 20 | 20 | | 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. | 21 | 21 | | 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 | | 22 | 22 | | 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) | | 23 | 24 | | 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 | | 24 | 25 | | 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 | | 25 | 26 | | 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) | | |
Collapse file Copy file name to clipboard +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboardExpand all lines: java/011_containerwithmostwater.java -20Lines changed: 0 additions & 20 deletions Collapse file Copy file name to clipboard +13-42Lines changed: 13 additions & 42 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 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 | 41 | 11 | else: | 42 | | - high -= 1 | 43 | | - return max_v | | 12 | + # remove left | | 13 | + left += 1 | | 14 | + return result |
|