首页 > 代码库 > [LeetCode]Candy, 解题报告
[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?
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; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。