题目:

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个rating数组,表示每个娃所得的评分。现在要求:
1.给每个娃至少分1个candy
2.分高的娃要比邻居所得的candy多
3.尽量少分candy

思路:
此题采用贪心法。

贪心策略:
1. 先分给谁? 从得分最少的娃开始分,因此不妨构造child对象,对得分排序,以此存入child。
2. 分多少? 由于是按照得分从低到高分candy的。 如果邻居已经有人得了candy,那么就分别将当前娃的得分与左右两边娃进行对比:
a.如果当前得分与左或右相等,只给1个
b.如果当前得分大于左或右,需要给对方所得的candy数+1个
综合a,b,取最大的那个。

如果两边没人得candy,只给当前娃1个。

实现代码:

class Child
{public Child(int i , int v){index = i;rating = v;}public int index;public int rating;
}public int Candy(int[] ratings)
{
if(ratings == null || ratings.Length == 0){return 0;
}if(ratings.Length == 1){return 1;
}var arr = new List<Child>();
var candies = new int[ratings.Length];for(var i = 0 ;i < ratings.Length; i++){candies[i] = 0;arr.Add(new Child(i, ratings[i]));
}arr = arr.OrderBy(x=>x.rating).ToList();for(var i = 0 ;i < arr.Count ; i++){var index = arr[i].index;if(candies[index] != 0){continue;}if(index == 0){candies[index] = ratings[index] == ratings[index + 1]  ? 1 : candies[index + 1] + 1;}else if(index == candies.Length - 1){candies[index] = ratings[index] == ratings[index - 1]  ? 1 : candies[index - 1] + 1;}else{var left = ratings[index-1] == ratings[index] ? 1 : candies[index - 1] + 1;var right = ratings[index+1] == ratings[index] ? 1 : candies[index + 1] + 1;candies[index] = Math.Max(left , right);}}var s = 0;
for(var i = 0;i < candies.Length; i++){s += candies[i];
}return s;
}

LeetCode -- Candy相关推荐

  1. LeetCode—Candy(分糖果)—java

    题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  2. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  3. linux美元符号切换为井号,MyBatis中井号与美元符号的区别

    WebSphere性能优化的几个方法 1.更改http server的配置文件参数KeepAlive.     原因:这个值说明是否保持客户与HTTP SERVER的连接,如果设置为ON,则请求数到达 ...

  4. leetcode 135. Candy | 135. 分发糖果(原创图文详解,Java)

    题目 https://leetcode.com/problems/candy/ 题解 思路 首先,根据题意,这是一个分糖果问题.本题需要满足两个条件: 每个孩子至少有一个糖果 rating 值较大的孩 ...

  5. C#LeetCode刷题之#888-公平的糖果交换(Fair Candy Swap)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3758 访问. 爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝 ...

  6. 【重点】LeetCode 135. Candy

    LeetCode 135. Candy 博客转载自:http://www.cnblogs.com/grandyang/p/4575026.html 这个题一看就很难...但答案真是清新脱俗啊... S ...

  7. [LeetCode]135.Candy

    [题目] There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. Candy -- LeetCode

    原题链接:  http://oj.leetcode.com/problems/candy/   这道题用到的思路和 Trapping Rain Water 是一样的,用动态规划.基本思路就是进行两次扫 ...

  9. Java描述 LeetCode,135. Candy 分发糖果

    大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力,如有错误还请不吝赐教,万分感谢.一起支持原创吧 ...

最新文章

  1. html制作第二页,订单第二页.html
  2. Facebook 中国程序员之死
  3. Lync Server 2010的部署系列_第六章 安装配置拓扑生成器、前端Server、前端池
  4. uva 784 Maze Exploration
  5. (48)Verilog HDL UART发送设计
  6. linux下安装Jira6.3
  7. 计算机系统结构自学试卷,全国2004年4月高等教育自学考试计算机系统结构试题...
  8. 06-01 搭建持续集成平台 Jenkins
  9. Sizzle.selectors.relative [ 源代码分析 ]
  10. 人生长途,感受着不断拼搏...
  11. Java中ArrayList,Vector,LinkedList,HashMap,HashTable,HashSet对比及总结
  12. 关于Access的日期比较和空值判断
  13. GEE、USGS、地理空间数据云上下载武汉地区的影像数据
  14. 【三维地图】开发攻略 —— 详解“GeoJSON”技术和应用场景
  15. 表单格式化插件jquery.serializeJSON
  16. 如何查看已删除的微信聊天记录?教你两招,找到答案
  17. 成人高考 计算机英语作文,「2017成人高考高起专英语作文」求成人考试英语作文4篇!...
  18. 解决桌面图标无法拖动的方法
  19. 全国地铁数据爬取-python
  20. 使用kubekey的all-in-one安装K8S1.24及KubeSphere3.3

热门文章

  1. java 反射private_java使用反射强制给private字段赋值
  2. Microsoft Remote Desktop的安装使用
  3. 关于年轻人申请基金面上项目的一点思考
  4. python中稀疏矩阵的常用表示COO LIL CSR CSC【上篇】
  5. c++ 回调函数示例
  6. 求出1000以内所有完数(C语言)
  7. Java笔记016-房屋出租系统
  8. 【ASP.NET】VS2015连接SQL Server数据库,实现登录、注册
  9. Windows安装HBase2.0.0(单机版)
  10. showdialog 尝试读取或写入受保护的内存_修改电压和时序,超频上3733,十铨火神3200 16G套装内存评测...