Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my commit for issue 174 #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions LeetCode/174-Container-With-Most-Water/Java/Problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.



Example 1:


Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: 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.
Example 2:

Input: height = [1,1]
Output: 1


Constraints:

n == height.length
2 <= n <= 105
0 <= height[i] <= 104

5 changes: 5 additions & 0 deletions LeetCode/174-Container-With-Most-Water/Java/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# How to Run?

1. Go to the leetcode question site. [Link](https://leetcode.com/problems/container-with-most-water/description/)
2. Copy and paste the code.
3. Run or Submit to see the result.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions LeetCode/174-Container-With-Most-Water/Java/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int maxArea(int[] height) {
int maxarea = 0;
int left = 0;
int right = height.length - 1;
while (left < right) {
int width = right - left;
maxarea = Math.max(maxarea, Math.min(height[left], height[right]) * width);
if (height[left] <= height[right]) {
left++;
} else {
right--;
}
}
return maxarea;
}
}
Loading