Leetcode135 candy
来源:互联网 发布:网络映射计算机名 编辑:程序博客网 时间:2024/05/16 10:17
题目来源:leetcode
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.
思路:建立另一个数组存放当前应该存放的糖果数量,每当第id个小于前面的时候应该去更新往前更新
代码如下:通过率65%不知道为啥
class Solution { public int candy(int[] ratings) { int dp[]=new int[ratings.length];//记录每个位置的应该发的最少糖果数量 dp[0]=1; int sum=0; for(int i=1;i<dp.length;i++) { if(ratings[i]>ratings[i-1]) dp[i]=dp[i-1]+1; else if(ratings[i]==ratings[i-1]) dp[i]=1; else{ dp[i]=1; //dp[i-1]=2; if(dp[i-1]==1) for(int j=i-1;j>=0;j--){ if(ratings[j]>ratings[j+1]) dp[j]=dp[j+1]+1; else if(ratings[j]<ratings[j+1]) break; // if(ratings[j]==ratings[j+1]) // dp[j]=dp[j+1]; } } } for(int i=0;i<ratings.length;i++) {sum+=dp[i];} return sum; }}
阅读全文
0 0
- Leetcode135(Candy)
- leetcode135~Candy
- LeetCode135 Candy
- Leetcode135 candy
- LeetCode135—Candy
- Leetcode135——Candy
- Leetcode135: Simplify Path
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- 数据结构与算法_快速排序
- BZOJ 1008:越狱
- Arduino UNO + esp8266 通信过程记录
- bmon 带宽监视器
- Python- list转数字
- Leetcode135 candy
- Linux cached memory
- iOS之《Effective Objective-C 2.0》读书笔记(10)
- PHPMailer 使用 中文乱码
- Junit覆盖率插件—Eclemma
- java:String使用equals和==比较的区别
- 235. Lowest Common Ancestor of a Binary Search Tree
- P3383 【模板】线性筛素数
- Numpy学习小结(1)