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

Commit 4ceb9c0

Browse files
author
C5141506
committed
ContainerWithMaxWater leetcode problem no 11
1 parent 5fd77fd commit 4ceb9c0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package java_problem.twopointer
2+
3+
object ContainerWithMostWater11 {
4+
@JvmStatic
5+
fun main(args: Array<String>) {
6+
val list = intArrayOf(1,8,6,2,5,4,8,3,7)
7+
val ls = maxArea(list)
8+
println(ls)
9+
}
10+
11+
fun maxArea(height: IntArray): Int {
12+
val n = height.size
13+
var start = 0
14+
var end = n - 1
15+
var max = 0
16+
while (start < end) {
17+
val minHeight = Math.min(height[start], height[end])
18+
val towerDiff = end - start
19+
val waterCapacity = towerDiff * minHeight
20+
if (max < waterCapacity) max = waterCapacity
21+
//find next max tower height
22+
if (height[end] > height[start]) start++ else end--
23+
}
24+
return max
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /