Leetcode candy中c++解法
2016-06-26 12:24
381 查看
/********************************************************************************** * * 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? * * **********************************************************************************/ 此题解法很巧妙,首先将权重数组从做到又进行处理,只要大于前一个权重,则使得分配数加1 例如5, 6, 7, 4, 1, 2, 3, 2, 1, 7是一个权重数组 则 1 2 3 1 1 2 3 1 1 2 然后从右向左即可做出统计total的值 #include <stdlib.h> #include <time.h> #include <iostream> #include <vector> using namespace std; void print(vector<int> &v); /* * The soluiton is O(2n) run-time complexity * * For example: * * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 } * * 1) Go through the ratings from left to right. * * Find the each increasing sub-array, giving the minimal candy * * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 } * ------> -> ------> -> ---> * candy[] = { 1, 2, 3, 1, 1, 2, 3, 1, 1, 2 } * * 2) Go through the raings from right to left. * * ratings[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 } * <- <- <------ <- <------ <- * prev_candy[] = { 1, 2, 3, 1, 1, 2, 3, 1, 1, 2 } * +1 +1 * candy[] = { 1, 2, 3, 2, 1, 2, 3, 2, 1, 2 } * * 3) total candy is 19 * */ int candy(vector<int> &ratings) { vector<int> candyCnt(ratings.size()) ; //allocate candies, considering the minimal rating on the left candyCnt[0] = 1; for(int i = 1; i < ratings.size(); i++){ candyCnt[i] = ratings[i] > ratings[i-1] ? candyCnt[i-1]+1 : 1; } print(candyCnt); //modify the allocation, considering the minimal rating on the right int totalCandy = candyCnt[ratings.size()-1]; for(int i = ratings.size()-2; i >= 0; i--){ candyCnt[i] = (ratings[i] > ratings[i+1] && candyCnt[i+1]+1 > candyCnt[i]) ? candyCnt[i+1]+1 : candyCnt[i]; //count total candies by the way totalCandy += candyCnt[i]; } print(candyCnt); return totalCandy; } void generateRatings(vector<int> &ratings, int n) { srand(time(0)); for (int i=0; i<n; i++) { ratings.push_back(rand()%10); } } void print(vector<int> &v) { for(int i=0; i<v.size(); i++){ cout << v[i] << " "; } cout << endl; } int main(int argc, char**argv) { int n = 10; if (argc>1){ n = atoi(argv[1]); } vector<int> ratings; generateRatings(ratings, n); print(ratings); cout << candy(ratings) << endl; cout << "--------------------" << endl; int r[] = { 5, 6, 7, 4, 1, 2, 3, 2, 1, 7 }; vector<int> ra(r, r+sizeof(r)/sizeof(r[0])); print(ra); cout << candy(ra) << endl; return 0; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解