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 d278cc5

Browse files
committed
tailrec
1 parent ac1a380 commit d278cc5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

‎11_container_with_most_water.scala‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
object Solution {
2+
3+
def maxArea(height: Array[Int]): Int = {
4+
@annotation.tailrec
5+
def maxAreaNoArray(beg: Int, end: Int, maxarea: Int): Int = {
6+
if (end - beg < 2) maxarea else{
7+
val headHeight = height(beg)
8+
val lastHeight = height(end - 1)
9+
if (headHeight < lastHeight){
10+
maxAreaNoArray(beg + 1, end, (maxarea max (headHeight * (end - beg - 1))))
11+
}else{
12+
maxAreaNoArray(beg, end - 1, (maxarea max (lastHeight * (end - beg - 1))))
13+
}
14+
}
15+
}
16+
17+
maxAreaNoArray(0, height.length, 0)
18+
}
19+
}

0 commit comments

Comments
(0)

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