leetcode Candy
来源:互联网 发布:编程的思想与原则 编辑:程序博客网 时间:2024/05/16 17:36
Candy
Total Accepted: 17540 Total Submissions: 92484My SubmissionsThere 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?
Discuss
/** * Abstract each slope, and sort by the length of the slop. For those slop whose length is one, then add one from * it bottom. The candy is the sum from the bottom to ceil. * When abstract the slope, we should node the length of the length of the before slop. * 1. if the ratings equals to their neighbors? I think it should be in just one. * * *///9:38->the equals should get just 1,not equal than its neighbor.10:00 finished.class Solution {public: int candy(vector<int> &ratings) { int minCandy = 0,tmp=0; int i=0,len = ratings.size(); // those who's rating equals to it's neighbors should be just one vector<int> candies(len,1); int candy = 1; candies[0] = 1; // from left to right for(i=1;i<len;i++) { if(ratings[i]>ratings[i-1]) { candies[i] = candies[i-1]+1; } } // from right to left for(i=len-2;i>=0;i--) { if(ratings[i]>ratings[i+1]) { tmp = candies[i+1]+1; if(tmp>candies[i]) { candies[i] = tmp; } } } // add the sum for(i=0;i<len;i++) { minCandy += candies[i]; } return minCandy; }};
0 0
- Leetcode: Candy
- LeetCode:Candy
- [leetcode]Candy
- Leetcode: Candy
- 【leetcode】Candy
- LeetCode:Candy
- [LeetCode] Candy
- [LeetCode]Candy
- [LeetCode] Candy
- 【LeetCode】Candy
- LeetCode | Candy
- Leetcode: Candy
- Candy - LeetCode
- leetcode Candy
- LeetCode Candy
- candy leetcode
- Candy -- LeetCode
- [LeetCode] Candy
- 交互设计师
- JAVA card 应用开发(九)APDU脚本命令中MAC的校验
- 类成员函数的重载、覆盖和隐藏区别?
- 《大话处理器》Cache一致性协议之MESI
- C++ 实现代理类
- leetcode Candy
- DLL 中 .def文件的使用
- UIWebView内存占用过高、崩溃、内存溢出
- stm32 开发板 usb DFU 实现 以及注意事项
- Linux 2.6.35 新增特性 RPS RFS [复制链接]
- 四、Java设计模式——模板模式
- MongoDB、iOS版新浪微博、高性能并发处理课程全新上线
- JQuery EasyUi之界面设计——通用的JavaScript(二)
- leetcode Gas Station