-
Notifications
You must be signed in to change notification settings - Fork 0
/
Candy.cpp
52 lines (45 loc) · 1.63 KB
/
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
51
52
/*
* There are N children standing in a line. Each child is assigned a rating value.
*
* You are giving candies to these children subjected to the following requirements:
*
* Each child must have at least one candy.
* Children with a higher rating get more candies than their neighbors.
* What is the minimum candies you must give?
*/
/*
* Use two way scan:
* 1) First scan: detect ascending ratings and increases candies accordingly;
* (adjust the order of the left neighbour)
* 2) Second scan: reversely detect ascending ratings and increases candies accordingly;
* (adjust the order of the right neighbour)
* This way we can handle the case of tie ratings (keep them as small as possible)
*/
class Solution {
public:
int candy(vector<int> &ratings) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (ratings.size() == 0)
return 0;
int n = static_cast<int> (ratings.size());
int sum = 0;
vector<int> assigned(n, 0);
// forward scan
for (int i = 0; i < n - 1; i++) {
if (ratings[i] < ratings[i + 1]) {
assigned[i + 1] = assigned[i] + 1;
}
}
// backward scan
for (int i = n - 1; i > 0; i--) {
if (ratings[i] < ratings[i - 1]) {
assigned[i - 1] = max(assigned[i] + 1, assigned[i - 1]);
}
}
for (int i = 0; i < n; i++) {
sum += assigned[i];
}
return sum + n;
}
};