-
Notifications
You must be signed in to change notification settings - Fork 1
/
Candy.cpp
56 lines (49 loc) · 1.34 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
53
54
55
56
//Last Modified: 2014/12/18
//Author: Junbo Xin
/*
*******************************Problem Description*********************************
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?
*/
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
int candy(vector<int> &ratings) {
int n = ratings.size();
if(n < 0) return 0;
vector<int> candy(n, 1);
//from begin to end, bottom is 1
for(int i = 1; i < n; ++i){
if(ratings[i] > ratings[i-1])
candy[i] = candy[i-1] + 1;
}
//from end to begin, bottom is 1, top is the bigger one
for(int i = n - 2; i >=0; --i){
if(ratings[i] > ratings[i+1]){
int temp = candy[i+1] + 1;
candy[i] = max(candy[i], temp);
}
}
int result = 0;
for(int i = 0; i < n; ++i)
result += candy[i];
return result;
}
};
int main(void){
Solution s;
int arr [] = {3,1,4,5,4,3,2,1,5,6};
vector<int> input;
for(int i = 0; i < sizeof(arr)/sizeof(int); ++i)
input.push_back(arr[i]);
int res = s.candy(input);
cout << res << endl;
getchar();
return 0;
}