-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path135.分发糖果.java
43 lines (36 loc) · 906 Bytes
/
135.分发糖果.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* @lc app=leetcode.cn id=135 lang=java
*
* [135] 分发糖果
*/
// @lc code=start
class Solution {
public int candy(int[] ratings) {
// According to description, ratings.length > 0
int sum = 1;
int candy = 1;
int incLen = 1;
int decLen = 0;
for (int i = 1; i < ratings.length; i++) {
if (ratings[i] >= ratings[i - 1]) {
decLen = 0;
if (ratings[i] == ratings[i - 1]) {
candy = 1;
} else {
candy++;
}
incLen = candy;
sum += candy;
} else {
decLen++;
if (decLen == incLen) {
decLen++;
}
sum += decLen;
candy = 1;
}
}
return sum;
}
}
// @lc code=end