Movatterモバイル変換


[0]ホーム

URL:


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

Commitbb11983

Browse files
author
applewjg
committed
Container With Most Water
Change-Id: I39bedb7e2f25d96af418a357d40ccf7c4cfe7f0b
1 parent4d02acd commitbb11983

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎ContainerWithMostWater.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Container With Most Water
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/container-with-most-water/
7+
Notes:
8+
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).
9+
n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).
10+
Find two lines, which together with x-axis forms a container, such that the container contains the most water.
11+
Note: You may not slant the container.
12+
13+
Solution: From both sides to the center.
14+
*/
15+
16+
17+
publicclassSolution {
18+
publicintmaxArea(int[]height) {
19+
intleft =0,right =height.length -1;
20+
intres =0;
21+
while (left <right) {
22+
res =Math.max(res,Math.min(height[left],height[right]) * (right -left));
23+
if (height[left] >height[right]) --right;
24+
else ++left;
25+
}
26+
returnres;
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp