题目链接

题目大意:分糖果,每个小朋友都有一个ratings值,且每个小朋友至少都要有一个糖果,而且每个小朋友的ratings值如果比左右邻舍的小朋友的ratings值高,则其糖果数量也比邻舍的小朋友多。

法一:超时。按照要求,从前往后比较每个小朋友的ratings值,如果后一个小朋友的ratings值比前一个大,则更新小朋友糖果值dp[i]=dp[i-1]+1;否则,将当前小朋友的糖果值置1,然后考察其前一个小朋友糖果值是否满足dp[i-1]<=dp[i],且ratings[i-1]>ratings[i],如果满足这两个条件,则说明前小朋友的糖果值需要更新,且需要循环遍历更新前面小朋友的糖果值。o(n^2)。代码如下:

     public int candy(int[] ratings) {
int len = ratings.length;
if(len == 0) {
return 0;
}
else if(len == 1) {
return 1;
}
int[] dp = new int[len];
//初始化第一个小伙伴的糖果值
dp[0] = ratings[0] <= ratings[1] ? 1 : 2;
int cnt = 0;
for(int i = 1; i < len; i++) {
//只与前面小伙伴的ratings进行比较
if(ratings[i] > ratings[i - 1]) {
dp[i] = dp[i - 1] + 1;
}
else {
dp[i] = 1;
//如果前面小伙伴的糖果是1,且ratings比较高,则遍历其前面的所有小伙伴
if(dp[i - 1] == 1 && ratings[i - 1] > ratings[i]) {
//更新前面的小伙伴的糖果值,因为这个更新,对于这种用例5,3,1,这个小伙伴前面的所有糖果值都要更新,所以进入下面的for循环进行判断
dp[i - 1] = 2;
for(int j = i - 2; j >= 0; j--) {
//如果前面的小伙伴的糖果值小,且ratings又比较高,则更新其值
if(ratings[j] > ratings[j + 1] && dp[j] <= dp[j + 1]) {
dp[j] = dp[j + 1] + 1;
}
else {
break;
}
}
}
}
}
//计算所有的糖果值
for(int i = 0; i < len; i++) {
cnt += dp[i];
}
return cnt;
}

法二(借鉴):两个数组,一个数组从前往后遍历,一旦ratings值比前一个大,则dp[i]=dp[i-1]+1;一个数组从后往前遍历,一旦ratings值比后一个大,则dp[i]=dp[i+1]+1。最后从这两个数组中取一个较大者,计算最终糖果值。o(n)。当然也可以用一个数组,但是思想逻辑都是一样的,只是如果用一个数组的话,就是从后往前遍历得到的新值与当前数组值进行比较,取较大者就是了。代码如下(耗时5ms):

     public int candy(int[] ratings) {
int len = ratings.length;
if(len == 0) {
return 0;
}
else if(len == 1) {
return 1;
}
int[] left_candy = new int[len];
int[] right_candy = new int[len];
//初始化
left_candy[0] = ratings[0] <= ratings[1] ? 1 : 2;
right_candy[len - 1] = ratings[len - 1] <= ratings[len - 2] ? 1 : 2;
//从前往后遍历
for(int i = 1; i < len; i++) {
if(ratings[i] > ratings[i - 1]) {
left_candy[i] = left_candy[i - 1] + 1;
}
else {
left_candy[i] = 1;
}
}
//从后往前遍历
for(int j = len - 2; j >= 0; j--) {
if(ratings[j] > ratings[j + 1]) {
right_candy[j] = right_candy[j + 1] + 1;
}
else {
right_candy[j] = 1;
}
}
//两者中取较大者,计算糖果值
int cnt = 0;
for(int i = 0; i < len; i++) {
cnt += Math.max(left_candy[i], right_candy[i]);
}
return cnt;
}

法三(借鉴):最优解。只遍历一遍,o(n)。一旦遍历到递减rating,则计数递减的个数,而暂停计算其糖果值;当遍历到递增rating时,则开始计算前面递减的小朋友的糖果值,以及当前小朋友的糖果值。具体注释看代码。代码如下(耗时5ms):

     public int candy(int[] ratings) {
int first = 1, cnt = 0, res = 1, len = ratings.length;
for(int i = 1; i < len; i++) {
//如果比前一个小朋友rating高,则计算总糖果值
if(ratings[i] >= ratings[i - 1]) {
//如果当前小朋友前面有递减rating,先处理这几个小朋友的糖果值
if(cnt > 0) {
//从递减的第二个数开始,到最后一个递减rating结束为止,这几个小朋友的糖果总值就是cnt * (cnt + 1) / 2
res += cnt * (cnt + 1) / 2;
//处理开始递减的第一个数,即将其需要增加的糖果数cnt-res+1,加入res中
if(cnt >= first) {
res += cnt - first + 1;
}
//重置
cnt = 0;
first = 1;
}
//对于当前第i个小朋友,正常计算其糖果值,将其加入res结果中
first = (ratings[i] == ratings[i - 1]) ? 1 : first + 1;
res += first;
}
//计数递减rating的个数
else {
cnt++;
}
}
//处理最后一组递减rating,而其后没有再反弹的小伙伴,即一直递减,不满足ratings[i] >= ratings[i - 1]就到数组终结
if(cnt > 0) {
res += cnt * (cnt + 1) / 2;
if(cnt >= first) {
res += cnt - first + 1;
}
}
return res;
}

135.Candy---贪心的更多相关文章

  1. LeetCode 135 Candy(贪心算法)

    135. Candy There are N children standing in a line. Each child is assigned a rating value. You are g ...

  2. leetcode 135. Candy ----- java

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  3. Leetcode#135 Candy

    原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...

  4. (LeetCode 135) Candy N个孩子站成一排,给每个人设定一个权重

    原文:http://www.cnblogs.com/AndyJee/p/4483043.html There are N children standing in a line. Each child ...

  5. 135. Candy

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

  6. [leet code 135]candy

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

  7. 【LeetCode】135. Candy

    Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  8. 135. Candy(Array; Greedy)

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  9. Java for LeetCode 135 Candy

    There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...

  10. 135 Candy 分配糖果

    There are N children standing in a line. Each child is assigned a rating value.You are giving candie ...

随机推荐

  1. 程序猿必备技能:数据库管理——关于MySQL

    一.初识MySQL 1.什么是数据库? 数据库(Database,DB)简而言之就是存放数据的仓库,是为了实现一定目的,按照某种规则组织起来的数据的集合. 2.使用数据库的必要性 (1)结构化存储大量 ...

  2. vdbench测试过程中遇到的小问题

    1.报Slave hd2-0 prematurely terminated 错误 首先根据提示查看hd2-0.stdout.html文件获取更多的错误信息,这个问题一般是未安装vdbench或者路径不 ...

  3. CPP 替代 PIL 图片处理(缩略图生成)

    python中使用PIL(Pyhton Image Library)进行图片处理,好处就是编写简单方便,但是不能很好利用机器多核的特点,于是在项目中决定使用cpp来实现图片处理. 项目中的图片处理主要 ...

  4. 配置用户通过Telnet登录设备的身份认证(AAA本地认证)

    背景信息 用户通过Telnet登录设备时,设备上必须配置验证方式,否则用户无法成功登录设备.设备支持不认证.密码认证和AAA认证三种用户界面的验证方式,其中AAA认证方式安全性最高. 采用AAA本地认 ...

  5. PD模型创建完获取生成表脚本

    1.双击表名,弹出属性对话框-->General----> Owner 表名前缀,如XX.SYS_TABLE  最好去掉 2.Preview 复制里面的脚本到数据库执行下即可

  6. Win10如何搭建FTP服务器以实现快速传输文件

    原文链接地址:http://blog.csdn.net/bai_langtao/article/details/77751447 Win10如何搭建FTP服务器以实现快速传输文件?相信大家在工作或生活 ...

  7. (转)搭建本地 8.8 W 乌云漏洞库

    下载地址: 开源地址: https://github.com/m0l1ce/wooyunallbugs 百度网盘: 链接: http://pan.baidu.com/s/1nvkFKox 密码: 94 ...

  8. 框架----Django框架(进阶篇)

    一.Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层 ...

  9. Oracle 解决【ORA-01704:字符串文字太长】(转)

    错误提示:oracle在toad中执行一段sql语句时,出现错误‘ORA-01704:字符串文字太长’.如下图: 原因:一般为包含有对CLOB字段的数据操作.如果CLOB字段的内容非常大的时候,会导致 ...

  10. libuv的多线程之间传递消息

    官网上给出的例子http://nikhilm.github.io/uvbook/threads.html#inter-thread-communication,中文理解在后边 Inter-thread ...