当前位置: 首页 > news >正文

LeetCode -- 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?


就是给定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;
}


相关文章:

  • Leet -- Plus One
  • Leet -- Generate Parentheses
  • LeetCode -- Distinct Subsequences
  • LeetCode -- SpiralOrder
  • Windows 2003下成功配置IIS+Php+Mysql+Zend Optimizer+GD库+Phpmyadmin
  • LeetCode -- WordBreak II
  • Azure 证书配置错误: The service configuration file does not provide the certificate identification
  • Linux精彩一句话最新版
  • LeetCode -- Count Complete Tree Node
  • LeetCode -- Isomorphic Strings
  • LeetCode -- Balanced Binary Tree
  • Linux系统信息查看命令大全
  • LeetCode -- Merge Two sorted lists
  • 汇编语言程序设计的基本方法
  • LeetCode -- Binary Tree Zigzag Level Order Traversal
  • 深入了解以太坊
  • 《微软的软件测试之道》成书始末、出版宣告、补充致谢名单及相关信息
  • 【Leetcode】104. 二叉树的最大深度
  • 【Redis学习笔记】2018-06-28 redis命令源码学习1
  • Docker下部署自己的LNMP工作环境
  • hadoop入门学习教程--DKHadoop完整安装步骤
  • Making An Indicator With Pure CSS
  • PaddlePaddle-GitHub的正确打开姿势
  • React-Native - 收藏集 - 掘金
  • Sequelize 中文文档 v4 - Getting started - 入门
  • SOFAMosn配置模型
  • 初识MongoDB分片
  • 聊聊redis的数据结构的应用
  • 批量截取pdf文件
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 如何优雅地使用 Sublime Text
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 学习使用ExpressJS 4.0中的新Router
  • 阿里云重庆大学大数据训练营落地分享
  • (pojstep1.1.2)2654(直叙式模拟)
  • (生成器)yield与(迭代器)generator
  • (学习日记)2024.02.29:UCOSIII第二节
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • (转)EOS中账户、钱包和密钥的关系
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .net core 3.0 linux,.NET Core 3.0 的新增功能
  • .NET NPOI导出Excel详解
  • @CacheInvalidate(name = “xxx“, key = “#results.![a+b]“,multi = true)是什么意思
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • [ 数据结构 - C++] AVL树原理及实现
  • [100天算法】-二叉树剪枝(day 48)
  • [20170705]lsnrctl status LISTENER_SCAN1
  • [Angular] 笔记 9:list/detail 页面以及@Output
  • [C#]无法获取源 https://api.nuge t.org/v3-index存储签名信息解决方法
  • [C/C++]数据结构----顺序表的实现(增删查改)
  • [C++] sqlite3_get_table 的使用
  • [C++] 多线程编程-thread::yield()-sleep_for()
  • [CentOs7]搭建ftp服务器(2)——添加用户
  • [DNS网络] 网页无法打开、显示不全、加载卡顿缓慢 | 解决方案