详细见:leetcode.com/problems/candy
Java Solution: github
package leetcode;
/*
* 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?
*/
public class P135_Candy {
public static void main(String[] args) {
int[] arr = tools.Random随机生成器.A_生成一个随机数据(100, 0, 900);
System.out.println(tools.Utils.LEETCODE_int_array_序列化_(arr));
}
/*
* 有这么简单的Hard题吗?
* 6 ms
* 9.28%
*/
static class Solution {
public int candy(int[] ratings) {
if (ratings == null || ratings.length == 0) {
return 0;
}
int[] forward = new int[ratings.length];
for (int i = 0; i < forward.length; i ++) {
if (i == 0 || ratings[i] <= ratings[i - 1]) {
forward[i] = 1;
} else {
forward[i] = forward[i - 1] + 1;
}
}
int[] backward = new int[ratings.length];
for (int i = backward.length - 1; i > -1; i --) {
if (i == backward.length - 1 || ratings[i] <= ratings[i + 1]) {
backward[i] = 1;
} else {
backward[i] = backward[i + 1] + 1;
}
}
int sum = 0;
for (int i = 0; i < backward.length; i ++) {
sum += Math.max(forward[i], backward[i]);
}
return sum;
}
}
/*
* 还是可以更快的。
* 5 ms
* 24.21%
*/
static class Solution2 {
public int candy(int[] ratings) {
if (ratings == null || ratings.length == 0) {
return 0;
}
int[] forward = new int[ratings.length];
for (int i = 0; i < forward.length; i ++) {
if (i == 0 || ratings[i] <= ratings[i - 1]) {
forward[i] = 1;
} else {
forward[i] = forward[i - 1] + 1;
}
}
int backward = 1, backward_pre = 1;
int ans = 0;
for (int i = forward.length - 1; i > -1; i --) {
if (i == forward.length - 1 || ratings[i] <= ratings[i + 1]) {
ans += forward[i];
backward_pre = 1;
} else {
backward = backward_pre + 1;
ans += Math.max(forward[i], backward);
backward_pre = backward;
}
}
return ans;
}
}
}
C Solution: github
/*
url: leetcode.com/problems/candy
AC 9ms 29.41%
*/
#include <stdio.h>
#include <stdlib.h>
int _max(int a, int b) {
return a < b ? b : a;
}
int candy(int* r, int rn) {
int * lc = (int*) malloc(sizeof(int) * rn);
int i = 0, sum = 0, p = 0, n = 0;
lc[0] = 1;
for (i = 1; i < rn; i ++) {
if (r[i] > r[i-1]) {
lc[i] = lc[i-1] + 1;
} else {
lc[i] = 1;
}
}
p = 1;
sum += _max(lc[rn-1], 1);
for (i = rn-2; i > -1; i --) {
if (r[i] > r[i+1]) {
n = p + 1;
} else {
n = 1;
}
sum += _max(lc[i], n);
p = n;
}
free(lc);
return sum;
}
int main() {
int r[] = {1, 2, 2};
int rn = 3;
printf("answer is %d\n", candy(r, rn));
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/candy
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年5月18日
@details: Solution: 89ms 68.38%
'''
class Solution(object):
def candy(self, r):
"""
:type r: List[int]
:rtype: int
"""
rn = 0 if r == None else len(r)
c, n, p, ans = [1] * rn, 0, 1, 0
for i in range(1, rn, 1):
if (r[i] > r[i-1]):
c[i] = c[i-1] + 1
else: c[i] = 1
ans += max(1, c[rn-1])
for i in range(rn-2, -1, -1):
if (r[i] > r[i+1]):
n = p+1
ans += max(c[i], n)
else:
n = 1
ans += max(c[i], n)
p = n
return ans