Java for LeetCode 135 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?

解题思路:

双向爬坡问题,从前往后、从后往前各扫一遍,JAVA实现如下:

	public int candy(int[] ratings) {
		int sum = 0;
		int[] candies = new int[ratings.length];
		for (int i = 0; i < ratings.length - 1; i++)
			if (ratings[i] < ratings[i + 1])
				candies[i + 1] = candies[i] + 1;
		for (int i = ratings.length - 2; i >= 0; i--)
			if (ratings[i + 1] < ratings[i])
				candies[i] = Math.max(candies[i], candies[i + 1] + 1);
		for (int i = 0; i < candies.length; i++)
			sum += 1 + candies[i];
		return sum;
	}
时间: 2024-11-07 17:03:49

Java for LeetCode 135 Candy的相关文章

LeetCode 135 Candy(贪心算法)

135. 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 mo

leetcode 135. Candy ----- 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 candies

LeetCode 135. Candy (O(n)时间 O(n)和O(1)空间的两种实现)

贪心算法,从局部最优推广成全局最优. 这里介绍O(n)时间 O(n)和O(1)空间的两种实现方法. O(n)时间 O(n)空间实现,参考了cnblog, 1957的解法 创建candy数组,初始化为1. 用pre_child_candy记录前一个孩子拿到的糖果数 1. 从左往右遍历 1) 如果ratings[i] > ratings[i-1], 那么candy[i] = ++ pre_child_candy, 亦即这个孩子比前一个孩子多拿一个糖果 2) 反之,candy[i] = pre_chi

leetcode[135] Candy

最少糖果问题.一排小孩,每个孩子有一个优先级,每个孩子至少要发给一个糖果,优先级高的比周围的孩子的糖果要多. 需要注意的是,优先级一样的没有要求说一样多糖果! 先初始化,每人一糖. 为了保证优先级大的比相邻的且优先级小的要糖果多.所以我们分两次处理,一次处理比左边的多,一次处理兼顾左边的多的情况下比右边的多.(如果优先级满足多的前提) 那么第一次从左到右,if (ratings[i] > ratings[i-1]) 那么cd[i] = cd[i-1]+1;    cd指糖果数组 从右边到左边需要

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

Java for LeetCode 098 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

Java for LeetCode 059 Spiral Matrix II

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 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下