题目:

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. 如何做好一场技术演讲-总结:3、如何把你的观点深深地刻在别人的脑海中?
  2. 负载均衡的几种常用方案
  3. 手机号验证_国际手机号收不到微博验证短信,微博验证短信一直提示超过上限怎么办?...
  4. 程序员面试题精选100题(12)-从上往下遍历二元树[数据结构]
  5. Intel Realsense D435 pyrealsense set_option() rs.option 可配置参数翻译
  6. 每天shell 之split
  7. LeetCode 79. Word Search
  8. C#实现DataTable按天分组并计数
  9. HtmlUnitDriver 网页内容动态抓取
  10. 收藏 | 《周志华机器学习详细公式推导版》发布,Datawhale开源项目pumpkin-book
  11. Shell 脚本 ssh免密码 登录 远程服务器 sshpass用法示例
  12. MXY-单点登陆系统
  13. 计算机大学职业规划2000字论文格式,大学生职业生涯规划2000字论文
  14. 23个热门python爬虫项目,爬虫仍需谨慎,牢饭不是很香!!!
  15. 华为光猫HG8120C的一些配置文件
  16. keil生成bin文件
  17. RabbitMQ的优势和劣势
  18. GC暂停时间过长——未关闭Swap
  19. WINDOWS安装.cab文件
  20. Linux和操作系统从入门到进阶2020最新书单大佬力荐

热门文章

  1. 信安软考——第六章 认证技术原理和应用 笔记记录
  2. .NET 巨人铸造的的长矛
  3. 2019年上半年软件设计师上午题
  4. 2023年上半年系统集成项目管理工程师什么时候报考?(附具体报名步骤)
  5. 2022年博客新星排行榜 日榜 2023-01-03 博客新星榜
  6. Learn OpenCV ---- 大津法(Otsu‘s)阈值
  7. 物联卡中心:三大运营商哪个流量便宜 联通物联卡资费
  8. 机器学习知识点:模型加权集成7种方法
  9. vue build报错 精辟
  10. WIN10 cmd使用cd命令无效