题目:

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 attacking ascending 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.

Example 1:

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.

Example 2:

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.

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.

分析1(原创-推荐):

class Solution {public int findPoisonedDuration(int[] timeSeries, int duration) {//给定一个攻击事件的时间点的数组,长度不超过10000,每次攻击持续性事件duration。//攻击持续事件只在当前时间点开始叠加,计算攻击一共持续的时间。//思路:迭代计算,当两个时间点有重复,减去即可。//假设没有交集的总的持续时间int sum=timeSeries.length*duration;//重叠时间int overlap=0;for(int i=1;i<timeSeries.length;i++){//求交叉部分if(timeSeries[i]<=(timeSeries[i-1]-1+duration)){//存在交叉overlap+=timeSeries[i-1]+duration-timeSeries[i];}}return sum-overlap;}
}

分析2(原创-对于数组长度过长容易超时):

class Solution {public int findPoisonedDuration(int[] timeSeries, int duration) {//给定一个攻击事件的时间点的数组,长度不超过10000,每次攻击持续性事件duration。//攻击持续事件只在当前时间点开始叠加,计算攻击一共持续的时间。//思路:采用一个数组记录时间点是否有攻击的效果,最后进行累加总的攻击事件即可。if(timeSeries.length==0||timeSeries==null) return 0;int len=timeSeries.length;//关键一步:取最后的攻击时间点加上持续时间作为最长长度。boolean [] flag=new boolean[timeSeries[len-1]+duration];//用于记录有多少个trueint count=0;for(int i=0;i<timeSeries.length;i++){for(int j=timeSeries[i]-1;j<timeSeries[i]-1+duration;j++){if(flag[j]==true){continue;}else{flag[j]=true;count++;}}}return count;}
}

LeetCode.495 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. leetcode 495.提莫攻击

    leetcode 495.提莫攻击 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希 ...

  5. 【Lintcode】1207. Teemo Attacking

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

  6. LeetCode 495. 提莫攻击

    1. 题目 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和提莫攻击 ...

  7. leetcode:495. 提莫攻击

    题目 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/teemo-attacking 在<英雄联盟>的世界中,有一个叫 " ...

  8. Java实现 LeetCode 495 提莫攻击

    495. 提莫攻击 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和 ...

  9. [LeetCode]495. 提莫攻击

    题目描述 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄.他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态. 当提莫攻击艾希,艾希的中毒状态正好持续 du ...

  10. Leetcode 495:提莫攻击

    题目描述 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和提莫攻击的 ...

最新文章

  1. Galaxy Release 20.05 发布,新增多项可视化体验
  2. leetcode_486. Predict the Winner
  3. lingo变量无限制版本_【运筹学】用Lingo求解运输问题,兼谈Lingo语法
  4. iOS App如何连接外设
  5. Elasticsearch 安装配置 外网访问 及 后台启动
  6. [JZOJ5426]摘Galo
  7. BZOJ3771 Triple(FFT+容斥原理)
  8. Huffman树进行编码和译码
  9. 大数据开发你需要知道的十个技术
  10. linux的基础知识——网络套接字函数
  11. 2009年5月14日
  12. Z-BlogPHP导航主题模版源码 绿色完美版
  13. 浅谈数学中的化归原则
  14. 【MySQL】ERROR 1055 (42000) ROUP BY clause this is incompatible with sql_mode=only_full_group_by
  15. Android PdfViewer预览pdf滚动或放大缩小时模糊然后正常的问题
  16. QScrollArea使用详解
  17. 基于点线特征的激光雷达+单目视觉里程计
  18. 2020年编程语言排行榜!
  19. git merge覆盖当前分支
  20. 有趣的小项目:半个指头大的收音机制作成功 单片机+RDA5807源程序

热门文章

  1. 生存分析之Cox模型简述与参数求解
  2. IDEA SpringBoot引入外部jar并打包
  3. 2021东南亚跨境电商平台之马来西亚热门电商平台排名TOP10
  4. 人工智能数学基础8:两个重要极限及夹逼定理
  5. 电脑技巧:电脑内存不足怎么办?看完你就会了!
  6. 完美解决桌面右键一直转圈,反应卡顿问题(重点是怎样删除workfolders)
  7. linux 实现离线迅雷,Linux下使用wget/aria2进行离线迅雷批量下载
  8. 花音机器人_氧叔在“难红难在哪儿”系列中分析曾黎时提到:
  9. 10.认证服务,单点登录
  10. Linux网络编程-UDP实现QQ聊天功能