4.19 leetcode -19 candy
来源:互联网 发布:googlenet in caffe 编辑:程序博客网 时间:2024/05/29 19:19
题目描述
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.
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?
妈的,这个题目没有想出来,真是草他大爷了,想的逻辑过于复杂,导致一直在调bug
贴一个网友的简单回答吧,话说这题属于动态规划,为啥没看到:
class Solution {public: int candy(vector<int> &ratings) { //题意:N个孩子站成一排,每个孩子分配一个分值。给这些孩子派发糖果,满足如下要求: //每个孩子至少一个 //分值更高的孩子比他的相邻位的孩子获得更多的糖果 //求至少分发多少糖果? int len=ratings.size(); if(len==1) return 1; int sum=0; vector<int> v(len,1);//初始将每个孩子的糖果数都设为1 //从左向右扫描,保证一个方向上分数更大的糖果更多 for(int i=1;i<len;i++){ if(ratings[i] > ratings[i-1]) v[i]=v[i-1]+1; } //从右向左扫描,保证另一个方向上分数更大的糖果更多 for(int i=len-2;i>=0;i--){ if(ratings[i] > ratings[i+1] && v[i] <= v[i+1]) v[i]=v[i+1]+1; } for(int i=0;i<len;i++){ sum+=v[i]; } return sum; }};
阅读全文
0 0
- 4.19 leetcode -19 candy
- 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
- Fortify SCA报告模板汇总
- webservice 的service.xml配置
- openjudge 二维数组右上坐下遍历(二维数组)
- 基于OpenCV实现最简单的数字识别
- Java自定义注解Annotation及取注解值的例子
- 4.19 leetcode -19 candy
- Android之ListView
- Time Complexity and Space Complexity
- openstack
- 数据插值
- 开源书籍:Android官方培训课程中文版
- 区块链2.0(六):以太坊—开放的智能合约完整解决方案
- ilnkBell隐私政策
- Android