吃苹果的最大数目

题目:吃苹果的最大数目

题目来源:https://leetcode-cn.com/problems/maximum-number-of-eaten-apples/

Java代码

public class Solution {
	public int eatenApples(int[] apples, int[] days) {
		int maxDay = 0;
		// 找出苹果还未全部腐烂的最后日期,maxDay
		for (int i = 0; i < days.length; i++) {
			maxDay = Math.max(i + days[i], maxDay);
		}
		// 下标值表示失效日期,存储"在当天后即腐烂的苹果数"
		int[] tmp = new int[maxDay];
		// curMax为当前的最后日期
		int curMax = maxDay, res = 0;
		for (int i = 0; i < maxDay; i++) {
			// 保存当天新长出的苹果
			if (i < apples.length && apples[i] != 0) {
				tmp[i + days[i] - 1] += apples[i];// 第*天过期的苹果数量增加apples[i]个
				curMax = Math.min(curMax, i + days[i] - 1);//
			}
			// 最先腐烂的苹果
			int idx = Math.max(i, curMax);
			while (idx < maxDay && tmp[idx] == 0) {
				idx++;
			}
			// 有苹果就拿出来吃掉
			if (idx != maxDay) {
				tmp[idx]--;// 当天腐烂的吃了一个
				res++;//结果加了个
			}
		}
		return res;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值