问题描述:

In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attackingascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition.

You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.

Note:

  1. You may assume the length of given time series array won't exceed 10000.
  2. You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000.

示例一:

Input: [1,4], 2
Output: 4
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately.
This poisoned status will last 2 seconds until the end of time point 2.
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
So you finally need to output 4.

示例二:

Input: [1,2], 2
Output: 3
Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned.
This poisoned status will last 2 seconds until the end of time point 2.
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status.
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3.
So you finally need to output 3.

问题来源:Teemo Attacking (详细地址:https://leetcode.com/problems/teemo-attacking/description/)

思路分析:题目那么一大片,吓死人啊都,但是题目的意思很简单,和Insert Interval 有点像,题目的意思就是数组中的每个元素表示每个时刻,然后后面的数字表示中毒持续的时间,最后求的是总共中毒的时间,题目我就不翻译了,在这拿两个例子解释下:

第一个例子:在第一分钟中毒,持续时间为2,所以可以持续到第三分钟,接着我们又在第四分钟施毒,又持续两分钟,这样就到第6分钟了,所以总共的中毒时间是4分钟。

第二个例子:在第一分钟中毒,本来是可以持续到第三分钟的,我在第二分钟再施一次毒,原来只可以到第三分钟的中毒时间,现在可以到第四分钟了,所以总共中毒时间为3分钟。

我们可以这样解答这道题:两个指针,一个为start,另外一个为end,如果新的时间片(是不是有点像Insert Interval中的那个new Interval?)的start大于正在执行的时间片的end,说明有交集,我们就更新它的end为新的值;相反,如果没有交集的话,我们就计算出上一段的时间片的大小,接着重新指定start和end指针。注意:记得最后还得把最后一次时间片的时间累加上。因为有重叠的时候计算的是上一次的时间片大小,而最后一次没有机会重叠了,所以记得加上!

代码:

复杂版:

简化版(其实也就是少写了一句代码):




Leetcode之Teemo Attacking 问题相关推荐

  1. LeetCode 495. Teemo Attacking

    题意:在LLP的世界里,有个英雄叫Teemo,他的攻击能使他的敌人Ashe处理中毒状态.现在,给出Teemo的攻击序列和每次攻击的中毒持续时间,输出Ashe中毒的总共时间. 思路:根据每次攻击时间,可 ...

  2. 495. Teemo Attacking

    495. Teemo Attacking 标签(空格分隔): leetcode array medium 题目 In LOL world, there is a hero called Teemo a ...

  3. 【LeetCode】495. Teemo Attacking(提莫大魔王)

    In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...

  4. 【Lintcode】1207. Teemo Attacking

    题目地址: https://www.lintcode.com/problem/teemo-attacking/description 在LOL中有一个叫"提莫"的英雄,假设被提莫攻 ...

  5. LeetCode刷题之python解法(持续更新)

    1. Two Sum 4行 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:d = {}fo ...

  6. LeetCode 力扣算法题解汇总,All in One

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: https://fuxuemingzhu.cn 关键词:LeetCode,力扣,算法,题解,汇总,解析 把自己刷过的所有题目做一个整理, ...

  7. Leetcode算法题-解法转载

    版权声明:本文为博主原创文章,未经博主允许不得转载.    https://blog.csdn.net/fuxuemingzhu/article/details/85112591 作者: 负雪明烛 i ...

  8. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

  9. LeetCode--495

    495. Teemo Attacking 题目概述 In LLP world, there is a hero called Teemo and his attacking can make his ...

  10. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

最新文章

  1. Android使用ListView控件问题
  2. 后退N帧协议-GBN
  3. 计算机乐谱弱点,缺点简谱郑源
  4. in 用不用索引_MySQL 索引最佳实践之问题反馈
  5. 关于table边框,设置了border-collapse:collapse之后,设置border-radius没效果
  6. 【C++面向对象】类的数据成员:绑定、布局和存取
  7. java stream intermediateterminal方法
  8. mysql 8.0配置主从同步_MySQL 8.0 主从同步
  9. c语言谭浩强第六章答案,C语言谭浩强版本第6章课后练习题答案..doc
  10. 一天一种设计模式之二-----备忘录模式
  11. 使用 virt-install 创建虚拟机
  12. codeforces 690D2 D2. The Wall (medium)(组合数学)
  13. 耶鲁大学 博弈论(Game Theory) 笔记1
  14. 技术领导力:作为技术团队领导经常为人所忽略的技能和职责
  15. C/C++中生成随机序列——随机函数的选择和自我实现
  16. 计算机应用 胡丹,正高级
  17. 【OpenCV】Linux 下用 g++ 编译 OpenCV 程序
  18. 第二章:泥潭中的群体
  19. Theo Mandel在其关于界面设计的著作中提出的3条黄金分割
  20. 期货价格和点价(期货 点价)

热门文章

  1. 代码解析 | '树'的数据结构转化
  2. win10关机慢!windows10关机慢
  3. 如何细分目标客户群体,让邮件效果更好?
  4. 强化学习DQN 入门小游戏 最简单的Pytorch代码
  5. java.io.IOException: Unable to read entire header; 275 bytes read; expected 512 bytes
  6. 详解JAVA实现支付宝接口编程
  7. 2018年的第一把火:直播问答的后博弈时代如何演进?
  8. 花音机器人_【扑杀花音攻略组】超弩级暗机器人攻略 (复刻X2)
  9. RPA手把手—— RPA 数据库篇 6 - MySQL 篇 2 - 增删改查 1
  10. android /data/system/dropbox,Android dropbox日志浅谈