[LeetCode][Java] 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?

题意:

N个小孩站成一列.每个小孩都有一个分数.

你按照以下的规则,把这些糖果分给这些小孩:

1. 每个小孩至少有一个糖.

2.得分高的小孩比他的邻居小孩有更多的糖果.

你能分出的最少的糖果的数目是多少.

算法分析:

*  根据题意,思路可以如下:

*  初始化数组dp,数组成员均为1,每个孩子先分配一个糖果

*  从左向右遍历,如果第i个孩子比第i - 1孩子等级高,则dp[i] = dp[i - 1] + 1

*  从右向左遍历,如果第i个孩子比第i + 1孩子等级高并且糖果比i+1糖果少,则dp[i] = dp[i + 1] + 1

*  这种思路实现的算法复杂度为O(n)

整体思路图如下参考博客http://blog.csdn.net/lanxu_yy/article/details/17752273

AC代码:

<span style="font-family:Microsoft YaHei;font-size:10px;">public class Solution
{
    public int candy(int[] ratings)
    {
        if (ratings == null || ratings.length == 0)
            return 0;

        ArrayList<Integer> dp = new ArrayList<Integer>();

        for (int i = 0; i < ratings.length; i++)
            dp.add(i, 1);

        for (int i = 1; i < ratings.length; i++)
            if (ratings[i] > ratings[i - 1])
                dp.set(i, dp.get(i - 1) + 1);

        for (int i = ratings.length - 2; i >= 0; i--)
            if (ratings[i] > ratings[i + 1] && dp.get(i) <= dp.get(i + 1))
                dp.set(i, dp.get(i + 1) + 1);

        int res = 0;

        for (int i = 0; i < dp.size(); i++)
            res += dp.get(i);

        return res;
    }
}</span>

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-10-12 20:30:05

[LeetCode][Java] Candy的相关文章

Candy leetcode java

题目: 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 cand

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的

Triangle leetcode java

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Word Ladder leetcode java

题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: