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

Commit9ec2375

Browse files
committed
trap
1 parent6ae97c9 commit9ec2375

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎array/Trap.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packageAlgorithms.array;
2+
3+
publicclassTrap {
4+
publicinttrap(int[]A) {
5+
if (A ==null) {
6+
return0;
7+
}
8+
9+
intmax =0;
10+
11+
intlen =A.length;
12+
int[]left =newint[len];
13+
int[]right =newint[len];
14+
15+
// count the highest bar from the left to the current.
16+
for (inti =0;i <len;i++) {
17+
left[i] =i ==0 ?A[i]:Math.max(left[i -1],A[i]);
18+
}
19+
20+
// count the highest bar from right to current.
21+
for (inti =len -1;i >=0;i--) {
22+
right[i] =i ==len -1 ?A[i]:Math.max(right[i +1],A[i]);
23+
}
24+
25+
// count the largest water which can contain.
26+
for (inti =0;i <len;i++) {
27+
intheight =Math.min(right[i],left[i]);
28+
if (height >A[i]) {
29+
max +=height -A[i];
30+
}
31+
}
32+
33+
returnmax;
34+
}
35+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp