[LeetCode]Candy, 解题报告

前言

回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服。花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改。此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧。

题目

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?

解题思路一

LeetCode上AC题目,还是首先读懂题意,这道题我WA了4次,2次是因为没有搞清楚题目到底需要做什么。

读懂题意可以判定这是一道一维动态规划题目,假设dp[i]代表第i个小孩发的糖果数:

当ratings[i] > ratings[i - 1],必须比前一个多给一块,dp[i] = dp[i - 1] + 1

当ratings[i] == ratings[i - 1], 两个排名相同,则当前给一块即可,dp[i] = 1

当ratings[i] < ratings[i - 1], 排名比上一个低,应该比上一个少一块,dp[i] = dp[i - 1] - 1; 但是考虑特殊的case,当dp[i - 1] == 1时,这时设置dp[i]为1,然后依次递归,for (int j = i - 1; j >= 0 && ratings[j] > ratings[j + 1]; j --) { dp[j] ++; }

思路给出,代码如下:

import java.util.Scanner;

public class Candy {
    public static int candy(int[] ratings) {
        if (ratings == null || ratings.length == 0) {
            return 0;
        }

        int[] dp = new int[ratings.length];
        dp[0] = 1;

        for (int i = 1; i < ratings.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 {
                if (dp[i - 1] > 1) {
                    dp[i] = dp[i - 1] - 1;
                } else {
                    dp[i] = 1;
                    for (int j = i - 1; j >= 0 && ratings[j] > ratings[j + 1]; j --) {
                        dp[j] ++;
                    }
                }
            }
        }

        int res = 0;
        for (int i = 0; i < dp.length; i ++) {
            res += dp[i];
        }

        return res;
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);

        while (cin.hasNext()) {
            int n = cin.nextInt();
            int[] ratings = new int[n];
            for (int i = 0; i < n; i++) {
                ratings[i] = cin.nextInt();
            }

            int res = candy(ratings);

            System.out.println(res);
        }

        cin.close();
    }
}

问题:

这样的时间复杂度在最差的情况下是O(n * n), 因此在处理大数据集时超时!

解题思路二

不能思维僵化,再次题解题目,题目的要达到的要求是:

  • 每个孩子都至少有一个糖果
  • 具有较高等级的孩子们比他左右的孩子获得更多的糖果

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

  • 初始化数组dp,数组成员均为1,每个孩子先分配一个糖果
  • 从左向右遍历,如果第i个孩子比第i - 1孩子等级高,则dp[i] = dp[i - 1] + 1
  • 从右向左遍历,如果第i个孩子比第i + 1孩子等级高并且糖果比i+1糖果少,则dp[i] = dp[i + 1] + 1

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

代码如下:

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;
    }
}

[LeetCode]Candy, 解题报告

时间: 2024-08-02 02:50:42

[LeetCode]Candy, 解题报告的相关文章

LeetCode: Permutations 解题报告

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. SOLUTION 1: 经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combination

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

【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 can

【原创】leetCodeOj --- Candy 解题报告

题目地址: https://leetcode.com/problems/candy/ 题目内容: Candy Total Accepted: 43150 Total Submissions: 203841 Difficulty: Hard There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected

Single Number | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/single-number/ 题目描述: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without usin

Add Two Numbers | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/add-two-numbers/ 题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i

Two Sum | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/two-sum/ 题目描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, whe

LeetCode: isSameTree1 解题报告

isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. SOLUTION 1 & SOLUTION 2: 以下是递归及非递归解法: 1. 递归解法就是判断当前节点是

LeetCode: Subsets 解题报告

Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1