题目:

There is a special kind of apple tree that grows apples every day for n days. On the ith day, the tree grows apples[i] apples that will rot after days[i] days, that is on day i + days[i] the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by apples[i] == 0 and days[i] == 0.

You decided to eat at most one apple a day (to keep the doctors away). Note that you can keep eating after the first n days.

Given two integer arrays days and apples of length n, return the maximum number of apples you can eat.

Example 1:

Input: apples = [1,2,3,5,2], days = [3,2,1,4,2]
Output: 7
Explanation: You can eat 7 apples:
- On the first day, you eat an apple that grew on the first day.
- On the second day, you eat an apple that grew on the second day.
- On the third day, you eat an apple that grew on the second day. After this day, the apples that grew on the third day rot.
- On the fourth to the seventh days, you eat apples that grew on the fourth day.

Example 2:

Input: apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
Output: 5
Explanation: You can eat 5 apples:
- On the first to the third day you eat apples that grew on the first day.
- Do nothing on the fouth and fifth days.
- On the sixth and seventh days you eat apples that grew on the sixth day.

Constraints:

  • apples.length == n
  • days.length == n
  • 1 <= n <= 2 * 10^4
  • 0 <= apples[i], days[i] <= 2 * 10^4
  • days[i] = 0 if and only if apples[i] = 0.

思路:

首先这题数据量不大,只有10的4次,所以完全能够接受遍历n。我用了哈希表存苹果要过期的时间和数量,记录要过期的最大时间。然后用三个int,分别是count,记录答案,最大能吃多少;cur,如果每天不吃的话现在手上的苹果量;act,如果当前能吃并且吃了一个的情况下,手上真实的苹果量。然后从0开始循环到找到的最大天数,每天就是往cur里加苹果和减苹果,如果当前手里大于等于1,则就吃一个,在count上记录。这样看上去是不是好像不用act?但是如果apples=[1],days=[2],那么没有act的情况下答案是2,这其实是最后一个test case,因为我们第二天吃的那个苹果已经在第一天被吃掉了,不可能再被吃一下,这就意味着我们需要一个act来记录真实的苹果情况。但是显然,我们不能直接用cur来记录,否则前面天天被吃,后面删减苹果的时候删减的是原本成熟时的数量,会导致cur变成负数。因此我们采用act,并且在每次更新cur的时候,把cur复制给act来更新act,这样既能记录理论上苹果数量,又可以知道真实的苹果数量,这一天能不能吃到。

代码:

class Solution {
public:
    int eatenApples(vector<int>& apples, vector<int>& days) {
        int time=0;
        unordered_map<int,int> out;
        for(int i=0;i<apples.size();i++)
        {
            int temp=i+days[i]+1;
            out[temp]+=apples[i];
            time = max(time, temp);
        }
        int count = 0, cur = 0,act=0;
        for (int i = 0; i <= time; i++)
        {
            if (out.count(i))
            {
                cur -= out[i];
                act = cur;
            }
            if (i - 1 < apples.size())
            {
                cur += apples[i - 1];
                act = cur;
            }
            if (act >= 1)
            {
                count += 1;
                act--;
            }
        }
        return count; 
    }
};

1705. Maximum Number of Eaten Apples相关推荐

  1. openstack创建实例报错Exceeded maximum number of retries

    Error: 实例 "vm2" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Exceeded maximum number of retries. Exceed ...

  2. Oracle 数据库设置最大进程数参数方法,oracle最大进程数满了处理方法,sysdba管理员登录报“maximum number of processes (150) exceeded“问题解决

    oracle 数据库使用 sysdba 管理员登录报: ORA-00020: maximum number of processes (150) exceeded 译:超过了最大进程数(150) 方法 ...

  3. Linux Increase The Maximum Number Of Open Files / File Descriptors (FD)

    http://www.cyberciti.biz/faq/linux-increase-the-maximum-number-of-open-files/ How do I increase the ...

  4. WCF:Maximum number of items that can be serialized or deserialized in an object graph is '65536'.

    错误 Maximum number of items that can be serialized or deserialized in an object graph is '65536'. Cha ...

  5. leetcode 330. Patching Array | 1798. Maximum Number of Consecutive Values You Can Make

    1798. Maximum Number of Consecutive Values You Can Make | 1798. 你能构造出连续值的最大数目 https://leetcode.com/p ...

  6. 321. Create Maximum Number 解题方法详解

    321. Create Maximum Number 题目描述 Given two arrays of length m and n with digits 0-9 representing two ...

  7. The number of requested virtual cores per node 3 exceeds the maximum number of virtual cores 2

    报错如下: yarn-session.sh  -tm 2048 -s 3 2020-06-08 22:24:20,317 WARN org.apache.flink.yarn.cli.FlinkYar ...

  8. ORA-00018: maximum number of sessions exceeded 超出最大会话数

    ORA-00018: maximum number of sessions exceeded ORA-00018: 超出最大会话数 Cause:       All session state obj ...

  9. tomcat调优方案Maximum number of threads (200) created for connector with address null and port 8091...

    1.tomcat6大并发出现:INFO: Maximum number of threads (200) created for connector with address null and por ...

最新文章

  1. 2019年中国智能制造的十大发展关键点
  2. 【新功能】开放搜索多路召回技术解读
  3. mysql 自身参照自身_MySQL入门
  4. Shell组件的返回码,0为成功,其他为失败.
  5. linux添加动态连接库,CentOS下如何添加动态链接库?
  6. 结合element-ui封装的一个分页函数
  7. 「leetcode」435. 无重叠区间【贪心算法】详细图解
  8. 局域网简单的SVN服务器的搭建
  9. QT 之ECharts加载shp方法
  10. 我就知道肯定有人想要这个
  11. python找房源_python抓取链家房源信息(三)
  12. python中for c in s是什么意思_以下程序s=3for c in Python:s=s+2print(s)的输出结果是:()...
  13. Redis Desktop Manager 运行时报0xc000007b
  14. v4l2接口解析和摄像头数据采集
  15. k8s编排nacos standalone模式踩坑实录
  16. 中国无尘室饮水机市场趋势报告、技术动态创新及市场预测
  17. adb shell查看进程提示grep不是内部命令或外部命令解决办法
  18. 线程、进程、多线程、多进程 和 多任务
  19. 入门交互设计的4个步骤
  20. 实验八 无线城域网WiMax仿真实验

热门文章

  1. Java获取当天的起始和结束时间
  2. 老黄怎么看AMD:双方差距已是9和0
  3. sqlserver清空表中的所有数据另类傻瓜方法
  4. 矩阵论基础知识——病态矩阵与条件数
  5. Windows配置MinGW及MinGW-make使用实例
  6. c语言--洛谷p1308统计单词数
  7. 【极客之作】快到极致的Android模拟器——Genymotion
  8. 安装maxscale实现MariaDB高可用及读写分离
  9. redhat 7中配置与管理WEB服务器
  10. 移动端游戏功耗优化方法(二)