-
Notifications
You must be signed in to change notification settings - Fork 2
/
135.Candy.cpp
50 lines (46 loc) · 1.47 KB
/
135.Candy.cpp
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
43
44
45
46
47
48
49
50
class Solution1 {
//https://leetcode.com/problems/candy/
public:
//第一种解法
int candy(vector<int>& ratings) {
if(ratings.empty()) return 0;
int n = ratings.size();
vector<int> table(n, 1);
for(int i=1;i<n;i++){
if(ratings[i]>ratings[i-1]) table[i]=table[i-1]+1;
}
int res=table[n-1];
for(int i=n-2;i>=0;i--){
if(ratings[i]>ratings[i+1]&&table[i]<=table[i+1]) table[i]=table[i+1]+1;
//杜绝 1 2 1 现象的发生
res+=table[i];
}
return res;
}
};
class Solution2 {
public:
//第二种解法
int candy(vector<int>& ratings) {
if(ratings.empty()) return 0;
int n = ratings.size();
vector<int> table(n, 1);
for(int i=1;i<n;i++){
if(ratings[i]>ratings[i-1]) table[i]=table[i-1]+1;
}
int res=table[n-1];
for(int i=n-2;i>=0;i--){
int cur=1;
if(ratings[i]>ratings[i+1]){
cur=table[i+1]+1;
}
res+=max(cur,table[i]);
table[i]=max(cur,table[i])
//table[i]=cur 或者 table[i]=max(cur,table[i]) 均可
// 没有“table[i]=cur;” 对 [1,3,1] 无影响
}
return res;
}
};
//第二种解法不知道,第一种方法较好,但是要判断&&table[i]<=table[i+1],因为已经前面比后面大的话,没有必要加1啦
// 一次AC,不准备再做了