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

Commit2198d4e

Browse files
committed
candy
1 parenteeaf7d0 commit2198d4e

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

‎dp/Candy.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,26 @@ public int candy(int[] ratings) {
66
return0;
77
}
88

9+
910
intlen =ratings.length;
10-
int[]candys =newint[len];
11+
int[]num =newint[len];
1112

12-
// go from the left side to the right side and give them candys.
13-
candys[0] =1;
14-
for (inti =1;i <len;i++) {
15-
if (ratings[i] >ratings[i -1]) {
16-
candys[i] =candys[i -1] +1;
13+
// go from left to right;
14+
for (inti =0;i <len;i++) {
15+
if (i >0 &&ratings[i] >ratings[i -1]) {
16+
num[i] =num[i -1] +1;
1717
}else {
18-
candys[i] =1;
19-
}
20-
}
21-
22-
for (inti =len -2;i >=0;i--) {
23-
if (ratings[i] >ratings[i +1]) {
24-
intnum =candys[i +1] +1;
25-
candys[i] =Math.max(candys[i],num);
18+
num[i] =1;
2619
}
2720
}
2821

22+
// go from right to left;
2923
intsum =0;
30-
for (inti:candys) {
31-
sum +=i;
24+
for (inti =len -1;i >=0;i--) {
25+
if (i <len -1 &&ratings[i] >ratings[i +1]) {
26+
num[i] =Math.max(num[i],num[i +1] +1);
27+
}
28+
sum +=num[i];
3229
}
3330

3431
returnsum;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp