LeetCode135 Candy


详细见: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
    





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值