1
+ // 规则定义: 设学生 A 和学生 B 左右相邻,A在 B左边;
2
+ // 左规则: 当 ratingsB>ratingsA时,B的糖比 A的糖数量多。
3
+ // 右规则: 当 ratingsA>ratingsB时,A的糖比 B的糖数量多。
4
+ // 相邻的学生中,评分高的学生必须获得更多的糖果 等价于 所有学生满足左规则且满足右规则。
5
+ // 两边扫描即可
6
+ // 链接:https://leetcode.cn/problems/candy/solution/candy-cong-zuo-zhi-you-cong-you-zhi-zuo-qu-zui-da-/
7
+
8
+ #include < algorithm>
9
+ #include < iostream>
10
+ #include < vector>
11
+ using namespace std ;
12
+ class Solution {
13
+ public:
14
+ int candy (vector<int >& ratings) {
15
+ const int n = ratings.size ();
16
+ // if(n == 0) return 0;
17
+ // if(n == 1) return 1;
18
+ vector<int > assign (n);
19
+ // 左扫描
20
+ int inc = 1 ; // 高分递增起点
21
+ for (int i = 1 ; i < n; i++) {
22
+ if (ratings[i] > ratings[i - 1 ]) {
23
+ assign[i] = max (inc++, assign[i]);
24
+ } else {
25
+ inc = 1 ;
26
+ }
27
+ }
28
+ // 右扫描
29
+ inc = 1 ;
30
+ for (int i = n - 2 ; i >= 0 ; i--) {
31
+ if (ratings[i] > ratings[i + 1 ]) {
32
+ assign[i] = max (inc++, assign[i]);
33
+ } else {
34
+ inc = 1 ;
35
+ }
36
+ }
37
+ // 最开始每人至少一颗
38
+ int sum = accumulate (&assign[0 ], &assign[0 ] + n, n);
39
+ return sum;
40
+ }
41
+ };
42
+ int main () {
43
+ // clang-format off
44
+ vector<int > ratings{ 5 , 4 , 1 , 9 , 7 , 2 , 3 , 2 , 1 , 7 };
45
+ // 0 0 0 1 0 0 1 0 0 1
46
+ // 2 1 0 2 1 0 2 1 0 1 //10+10
47
+ // vector<int> ratings{};
48
+ // 0 0 0 1 0 0 1 0 0 1
49
+ // 2 1 0 2 1 0 2 1 0 1 //10+10
50
+ // clang-format on
51
+ Solution solution;
52
+ cout << solution.candy (ratings) << ' \n ' ;
53
+ return 0 ;
54
+ }
55
+
1
56
// Source : https://oj.leetcode.com/problems/candy/
2
57
// Author : Hao Chen
3
58
// Date : 2014-10-12
4
59
5
- /* *********************************************************************************
6
- *
7
- * There are N children standing in a line. Each child is assigned a rating value.
8
- *
9
- * You are giving candies to these children subjected to the following requirements:
10
- *
11
- * Each child must have at least one candy.
12
- * Children with a higher rating get more candies than their neighbors.
13
- *
14
- * What is the minimum candies you must give?
15
- *
16
- *
17
- **********************************************************************************/
60
+ /* *********************************************************************************
61
+ *
62
+ * There are N children standing in a line. Each child is assigned a rating value.
63
+ *
64
+ * You are giving candies to these children subjected to the following requirements:
65
+ *
66
+ * Each child must have at least one candy.
67
+ * Children with a higher rating get more candies than their neighbors.
68
+ *
69
+ * What is the minimum candies you must give?
70
+ *
71
+ *
72
+ **********************************************************************************/
18
73
19
74
#include < stdlib.h>
20
75
#include < time.h>
21
76
#include < iostream>
22
77
#include < vector>
23
78
using namespace std ;
24
79
25
-
26
- void print (vector<int > &v);
80
+ void print (vector<int >& v);
27
81
28
82
/*
29
83
* The soluiton is O(2n) run-time complexity
30
84
*
31
85
* For example:
32
86
*
33
- * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 }
87
+ * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 }
34
88
*
35
89
* 1) Go through the ratings from left to right.
36
90
*
37
- * Find the each increasing sub-array, giving the minimal candy
91
+ * Find the each increasing sub-array, giving the minimal candy
38
92
*
39
- * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 }
93
+ * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 }
40
94
* ------> -> ------> -> --->
41
95
* candy[] = { 1, 2, 3, 1, 1, 2, 3, 1, 1, 2 }
42
96
*
43
97
* 2) Go through the raings from right to left.
44
98
*
45
- * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 }
99
+ * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 }
46
100
* <- <- <------ <- <------ <-
47
101
* prev_candy[] = { 1, 2, 3, 1, 1, 2, 3, 1, 1, 2 }
48
102
* +1 +1
@@ -51,56 +105,56 @@ void print(vector<int> &v);
51
105
* 3) total candy is 19
52
106
*
53
107
*/
54
- int candy (vector<int > &ratings) {
55
-
56
- vector<int > candyCnt (ratings.size ()) ;
57
- // allocate candies, considering the minimal rating on the left
58
- candyCnt[0 ] = 1 ;
59
- for (int i = 1 ; i < ratings.size (); i++){
60
- candyCnt[i] = ratings[i] > ratings[i-1 ] ? candyCnt[i-1 ]+1 : 1 ;
61
- }
62
- print (candyCnt);
63
- // modify the allocation, considering the minimal rating on the right
64
- int totalCandy = candyCnt[ratings.size ()-1 ];
65
- for (int i = ratings.size ()-2 ; i >= 0 ; i--){
66
- candyCnt[i] = (ratings[i] > ratings[i+1 ] && candyCnt[i+1 ]+1 > candyCnt[i]) ? candyCnt[i+1 ]+1 : candyCnt[i];
67
- // count total candies by the way
68
- totalCandy += candyCnt[i];
69
- }
70
- print (candyCnt);
71
- return totalCandy;
108
+ int candy (vector<int >& ratings) {
109
+ vector<int > candyCnt (ratings.size ());
110
+ // allocate candies, considering the minimal rating on the left
111
+ candyCnt[0 ] =
7E71
1 ;
112
+ for (int i = 1 ; i < ratings.size (); i++) {
113
+ candyCnt[i] = ratings[i] > ratings[i - 1 ] ? candyCnt[i - 1 ] + 1 : 1 ;
114
+ }
115
+ print (candyCnt);
116
+ // modify the allocation, considering the minimal rating on the right
117
+ int totalCandy = candyCnt[ratings.size () - 1 ];
118
+ for (int i = ratings.size () - 2 ; i >= 0 ; i--) {
119
+ candyCnt[i] = (ratings[i] > ratings[i + 1 ] && candyCnt[i + 1 ] + 1 > candyCnt[i])
120
+ ? candyCnt[i + 1 ] + 1
121
+ : candyCnt[i];
122
+ // count total candies by the way
123
+ totalCandy += candyCnt[i];
124
+ }
125
+ print (candyCnt);
126
+ return totalCandy;
72
127
}
73
128
74
- void generateRatings (vector<int > & ratings, int n) {
75
- srand (time (0 ));
76
- for (int i= 0 ; i< n; i++) {
77
- ratings.push_back (rand ()% 10 );
78
- }
129
+ void generateRatings (vector<int >& ratings, int n) {
130
+ srand (time (0 ));
131
+ for (int i = 0 ; i < n; i++) {
132
+ ratings.push_back (rand () % 10 );
133
+ }
79
134
}
80
135
81
- void print (vector<int > & v) {
82
- for (int i= 0 ; i< v.size (); i++){
83
- cout << v[i] << " " ;
84
- }
85
- cout << endl;
136
+ void print (vector<int >& v) {
137
+ for (int i = 0 ; i < v.size (); i++) {
138
+ cout << v[i] << " " ;
139
+ }
140
+ cout << endl;
86
141
}
87
142
88
- int main (int argc, char **argv)
89
- {
90
- int n = 10 ;
91
- if (argc>1 ){
92
- n = atoi (argv[1 ]);
93
- }
94
- vector<int > ratings;
95
- generateRatings (ratings, n);
96
- print (ratings);
143
+ int main (int argc, char ** argv) {
144
+ int n = 10 ;
145
+ if (argc > 1 ) {
146
+ n = atoi (argv[1 ]);
147
+ }
148
+ vector<int > ratings;
149
+ generateRatings (ratings, n);
150
+ print (ratings);
97
151
98
- cout << candy (ratings) << endl;
152
+ cout << candy (ratings) << endl;
99
153
100
- cout << " --------------------" << endl;
101
- int r[] = { 5 , 6 , 7 , 4 , 1 , 2 , 3 , 2 , 1 , 7 };
102
- vector<int > ra (r, r+ sizeof (r)/ sizeof (r[0 ]));
103
- print (ra);
104
- cout << candy (ra) << endl;
105
- return 0 ;
154
+ cout << " --------------------" << endl;
155
+ int r[] = {5 , 6 , 7 , 4 , 1 , 2 , 3 , 2 , 1 , 7 };
156
+ vector<int > ra (r, r + sizeof (r) / sizeof (r[0 ]));
157
+ print (ra);
158
+ cout << candy (ra) << endl;
159
+ return 0 ;
106
160
}
0 commit comments