开始leetcode day9:Candy
来源:互联网 发布:jenkins的数据库 编辑:程序博客网 时间:2024/06/06 04:08
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?
Java:
public class Solution {
public int candy(int[] ratings) {
int[] left = new int[ratings.length];
int[] right = new int[ratings.length];
int k = 1;
left[0] = 1;
for(int i=1; i<ratings.length; i++)
{
if(ratings[i] > ratings[i-1])
left[i] = ++k;
else
{
k = 1;
left[i] =k;
}
}
k = 1;
right[ratings.length - 1] = k;
int sum=Math.max(left[ratings.length - 1],right[ratings.length - 1]);
for(int j = ratings.length -2; j>=0;j--)
{
if(ratings[j] > ratings[j+1]) right[j] = ++k;
else
{
k=1;
right[j] = k;
}
sum = Math.max(left[j],right[j]) + sum;
}
return sum;
}
}
- 开始leetcode day9:Candy
- leetcode -day9 Candy & Gas Station & Binary Tree Maximum Path Sum
- 开始刷leetcode day9 : Reverse Linked List
- 开始刷题leetcode day9:Same Tree
- 开始刷leetcode day9: Maximum Depth of Binary Tree
- 开始刷leetcode day9:Remove Duplicates from Sorted List
- Leetcode: Candy
- LeetCode:Candy
- [leetcode]Candy
- Leetcode: Candy
- 【leetcode】Candy
- LeetCode:Candy
- [LeetCode] Candy
- [LeetCode]Candy
- [LeetCode] Candy
- 【LeetCode】Candy
- LeetCode | Candy
- Leetcode: Candy
- 使用postman和editthiscookies
- 一个典型的生产者和消费者的题目和实现
- IOS Assigning to 'id<UITabBarControllerDelegate>' from incompatible type...
- Heap+Dijkstra堆优化的Dijkstra
- 如何以root用户远程访问MySQL
- 开始leetcode day9:Candy
- JSON之JSONArray的使用
- Oracle字符集修改
- 引用类型(4)Function类型
- 开源 java CMS - FreeCMS2.3 积分记录
- 图像的乘法操作immultiply.m
- 二分图
- 引用类型(5)基本包装类型
- android-service