Leetcode Candy
来源:互联网 发布:linux脚本压缩文件 编辑:程序博客网 时间:2024/05/16 18:35
https://oj.leetcode.com/problems/candy/
/***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?*题目大概意思就是给你一些孩子的rating,这些孩子站成一个直线,相邻的孩子rating高的一定要比rating低的孩子的candy多*所有孩子至少有一个candy。*/#include <iostream>#include <vector>using namespace std; class Solution {public: int candy(vector<int> &ratings) { const int size = ratings.size(); vector<int> ratingValue(size, 1); int i = 0; int k = 1;//从左往右扫瞄一次,波谷为1,然后依次加1到波峰,再到波谷时又是1 for(i = 1; i < size; i++){ if(ratings[i] > ratings[i - 1]){ ratingValue[i] = max(ratingValue[i], ++k); } else { //从波峰到波谷时,不改变ratingValue,只是置K值 k = 1; } } //同理,从右往左扫瞄一次,波谷为1,然后依次加1到波峰 k = 1; for(i = size - 2; i >= 0; i--){ if(ratings[i] > ratings[i + 1]){ ratingValue[i] = max(ratingValue[i], ++k); } else { k = 1; } } int sumCandy = 0; for(i = 0; i < size; i++){ sumCandy = sumCandy + ratingValue[i]; } return sumCandy; }};int main(){Solution solution;int array[8] = {6, 3, 2, 8, 7, 6, 9, 4};vector<int> ratings(array, array+7);cout<<solution.candy(ratings)<<endl;}</span>
1 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
- 大数相乘算法
- Site Scope许可证分享
- IOS开发之---网络应用
- 买点折扣优站已成功添加API模版
- UVa 10056 - What is the Probability ?(等比数列求极限)
- Leetcode Candy
- 工具系列之OneNote--关于团队的知识管理平台
- 有向图求最小环
- HDU_ACM-2055 An easy problem
- C#-—体验属性的声明和访问
- [HDU 4779 Tower Defense] 递推
- ubuntu 什么是xinetd
- TextView显示图片
- HDU 1507 Uncle Tom's Inherited Land* 二分图的最大匹配