int candy(vector<int> &ratings) { int sum = 0; int number = ratings.size(); vector<int> candies(number, 1); // 每人至少1粒糖 for (int i = 1; i < number; ++i) { if (ratings[i] > ratings[i - 1]) { candies[i] = candies[i - 1] + 1; } } for (int i = number - 2; i >= 0; --i) { if (ratings[i] > ratings[i + 1]) { if (candies[i] <= candies[i + 1]) { candies[i] = candies[i + 1] + 1; } } } for (int i = 0; i < number; ++i) { sum += candies[i]; } return sum; }
int candy(vector<int> &ratings) { int length = static_cast<int>(ratings.size()); if (length <= 1) return length; vector<int> candies(ratings); int minScore = candies[0]; for (int i = 1; i < length; ++i) { if (candies[i] < minScore) { minScore = candies[i]; } } for (int i = 0; i < length; ++i) { candies[i] -= (minScore - 1); } bool dirty = true; while (dirty) { dirty = false; // Optimize the first. if (ratings[0] > ratings[1] && candies[0] > candies[1] + 1) { dirty = true; candies[0] = candies[1] + 1; } else if (ratings[0] <= ratings[1] && candies[0] > 1) { dirty = true; candies[0] = 1; } for (int i = 1; i < length - 1; ++i) { int minCandy; if (ratings[i] > ratings[i - 1] && ratings[i] > ratings[i + 1]) { minCandy = std::max(candies[i - 1], candies[i + 1]) + 1; } else if (ratings[i] <= ratings[i - 1] && ratings[i] <= ratings[i + 1]) { minCandy = 1; } else if (ratings[i] > ratings[i - 1] && ratings[i] <= ratings[i + 1]) { minCandy = candies[i - 1] + 1; } else { // ratings[i] <= ratings[i - 1] && ratings[i] > ratings[i + 1]. minCandy = candies[i + 1] + 1; } dirty = dirty || (candies[i] > minCandy); candies[i] = minCandy; } // Optimize the last. if (ratings[length - 1] > ratings[length - 2] && candies[length - 1] > candies[length - 2] + 1) { dirty = true; candies[length - 1] = candies[length - 2] + 1; } else if (ratings[length - 1] <= ratings[length - 2] && candies[length - 1] > 1) { dirty = true; candies[length - 1] = 1; } } int sum = 0; for (int i = 0; i < length; ++i) { sum += candies[i]; } return sum; }
int candy(std::vector<int> &ratings) { int n = ratings.size(); std::vector<int> candies (n, 1); for(int i=1; i<n; ++i) if(ratings[i]>ratings[i-1]) candies[i] = 1+candies[i-1]; for(int i= n-1; i>0; --i) if(ratings[i-1]>ratings[i] && candies[i-1]<=candies[i]) candies[i-1] = 1+candies[i]; return std::accumulate(candies.begin(), candies.end(), 0); }
int candy(vector<int> &ratings) { //double scan int n = ratings.size(); vector<int> candies(n, 1); for(int i=1; i<n; ++i) //children with a higher rating now get more candies than their left neighbors if(ratings[i] > ratings[i-1]) candies[i] = candies[i-1] + 1; for(int i=n-2; i>=0; --i) if(ratings[i] > ratings[i+1] && candies[i] <= candies[i+1]) //now get more candies than neighbors of both sides candies[i] = candies[i+1] + 1; return accumulate(candies.begin(), candies.end(), 0); }
int candy(vector<int> &ratings) { // Corner case: if (ratings.size( ) <= 1) { return ratings.size( ); // Zero or one. } vector<int> candies(ratings.size(), 1); // One candy per children at least. extraCandies(begin(candies), end(candies), // Extra candies, left to right. begin(ratings)); extraCandies(candies.rbegin(), candies.rend(), // Extra candies, right to left. ratings.rbegin()); return accumulate(begin(candies), end(candies), 0); // Total summ of candies. }
/** * @param ratings Children's ratings * @return the minimum candies you must give */ int candy(vector<int>& ratings) { vector<int> candies(ratings.size(), 1); for (int i = 1; i < ratings.size(); ++i) { if (ratings[i] > ratings[i - 1]) { candies[i] = candies[i - 1] + 1; } } for (int i = ratings.size(); i >= 0; --i) { if (ratings[i - 1] > ratings[i] && candies[i - 1] <= candies[i]) { candies[i - 1] = candies[i] + 1; } } return accumulate(candies.cbegin(), candies.cend(), 0); }
int candy(vector<int> &ratings) { int n = ratings.size(); vector<int> candies(n, 0); vector<pair<int,int> > children(n); // rating, pos for (int i = 0; i < n; ++i) children[i] = make_pair(ratings[i], i); sort(children.begin(), children.end()); for (int i = 0; i < children.size(); ++i) { int pos = children[i].second; int left = 0; if (pos > 0 && ratings[pos] > ratings[pos-1]) left = candies[pos-1]; int right = 0; if (pos+1 < n && ratings[pos] > ratings[pos+1]) right = candies[pos+1]; candies[pos] = max(left, right) + 1; } int total = 0; for (int i = 0; i < n; ++i) total += candies[i]; return total; }
int candy(vector<int> &ratings) { int size = ratings.size(); vector<int> candies(size, 1); for (int i = 1; i < size; i++) if (ratings[i - 1] < ratings[i]) candies[i] = candies[i-1] + 1; for (int i = size - 2; i >=0; i--) if (ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) candies[i] = candies[i + 1] + 1; int total = 0; for (int i = 0; i < size; i++) total += candies[i]; return total; }
int candy(vector<int>& ratings) { priority_queue<II, vector<II>, Comp> pq; for(int i = 0; i < ratings.size(); i++) { pq.push(make_pair(ratings[i], i)); } int ans = 0; vector<int> candies(ratings.size(), 0); while(!pq.empty()) { II u = pq.top(); pq.pop(); int candy = 1; if(u.second - 1 > -1 && ratings[u.second - 1] < u.first && candies[u.second - 1]) candy = max(candy, candies[u.second - 1] + 1); if(u.second + 1 < ratings.size() && ratings[u.second + 1] < u.first && candies[u.second + 1]) candy = max(candy, candies[u.second + 1] + 1); ans += candy; candies[u.second] = candy; } return ans; }
int candy(vector<int> &ratings) { // Note: The Solution object is instantiated only once and is reused by each // test case. int n = ratings.size(); vector<int> candies(n); candies[0] = 1; for (int i = 1; i < n; i++) { if (ratings[i] > ratings[i - 1]) { candies[i] = candies[i - 1] + 1; } else { candies[i] = 1; } } int sum = candies[n - 1]; for (int i = n - 2; i >= 0; i--) { if (ratings[i] > ratings[i + 1]) { candies[i] = max(candies[i], candies[i + 1] + 1); } sum += candies[i]; } return sum; }
std::vector<unsigned> give_candies(const std::vector<unsigned>& children) { if (children.size() == 0) { return {}; } std::vector<unsigned> candies(children.size()); std::fill(candies.begin(), candies.end(), 1); for (int i = 1; i < children.size(); ++i) { if (children[i] > children[i - 1]) { candies[i] = std::max(candies[i - 1] + 1, candies[i]); } } for (int i = children.size() - 2; i >= 0; --i) { if (children[i] > children[i + 1]) { candies[i] = std::max(candies[i + 1] + 1, candies[i]); } } return candies; }