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.

题目标签:Array

  题目给了我们一个timeSeries, 和一个 duration,让我们找出中毒的total 时间。

  居然和LOL 有关系,没玩过游戏的我,看来也可以去玩几把了。言归正传,这道题目只要设立一个start 和 end,遍历timeSeries,每遇到一个数字的时候,判断这个数字是不是在start 和 end 的范围里面,如果不在的话,直接加duration;如果在的话,只加前面一段的范围;更新start 和 end。注意一下最后还要把结尾的range 加进去。

  补充一下,今天正巧赶上 2017 lol world championships day 2, youtube 看到一个live 直播就点进去看了一下午,看着挺带劲的,以后有机会玩几把。

Java Solution:

Runtime beats 77.33%

完成日期:09/24/2017

关键词:Array

关键点:设立一个范围:start,end 并且维护这个范围

 class Solution
{
public int findPoisonedDuration(int[] timeSeries, int duration)
{
int totalTime = 0;
if(timeSeries == null || timeSeries.length == 0)
return totalTime;
// set up initial range
int start = timeSeries[0];
int end = start + duration;
// iterate from 2nd
for(int i=1; i<timeSeries.length; i++)
{ // not overlapped
if(timeSeries[i] > end)
totalTime += duration; // add duration
else // overlapped
totalTime += timeSeries[i] - start; start = timeSeries[i]; // update start and end
end = start + duration;
}
// add last range
totalTime += end - start; return totalTime;
}
}

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

LeetCode 495. Teemo Attacking (提莫攻击)的更多相关文章

  1. [LeetCode] 495. Teemo Attacking 提莫攻击

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

  2. 495 Teemo Attacking 提莫攻击

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

  3. [LeetCode] Teemo Attacking 提莫攻击

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

  4. 【LeetCode】495. Teemo Attacking 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 495. Teemo Attacking

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

  6. [Swift]LeetCode495. 提莫攻击 | Teemo Attacking

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

  7. Java实现 LeetCode 495 提莫攻击

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

  8. [Leetcode]495.提莫攻击

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

  9. leetcode解题报告(32):Teemo Attacking

    描述 In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poison ...

随机推荐

  1. paxos 算法原理学习

    下面这篇关于paxos分布式一致性的原理,对入门来说比较生动有趣,可以加深下影响.特此博客中记录下. 讲述诸葛亮的反穿越 0.引子 一日,诸葛亮找到刘备,突然献上一曲<独角戏>,而后放声大 ...

  2. lintcode 155 二叉树的最小深度

    二叉树的最小深度   描述 笔记 数据 评测 给定一个二叉树,找出其最小深度. 二叉树的最小深度为根节点到最近叶子节点的距离. 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Ai ...

  3. POJ-3045 Cow Acrobats (C++ 贪心)

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  4. Java Classloader机制解析(转)

    做Java开发,对于ClassLoader的机制是必须要熟悉的基础知识,本文针对Java ClassLoader的机制做一个简要的总结.因为不同的JVM的实现不同,本文所描述的内容均只限于Hotspo ...

  5. 举例让抽象问题具体化:包含min函数的栈

    定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.在该栈中,调用min.push及pop的时间复杂度都是O(1). import java.util.Stack; public c ...

  6. 谈谈浏览器http缓存

    请求头 user-agent pragma Cache-control Referer Accept Cookit If-Modified-Since If-None-Match 响应头 conten ...

  7. kvm 虚拟化的使用

    kvm原理:基于内核空间虚拟化,加载内核模块,来做到虚拟化(简称内核空间).基于qemu连接内核,driver驱动连接kvm的API接口(简称用户空间): hypervisor 管理硬件设备,传统的虚 ...

  8. 解决linux AMR转MP3出现转码成功却无法播放的问题

    根据帖子:http://blog.csdn.net/z313731418/article/details/50218341  的提示,在linux安装ffmpeg,确实在linux下使用命令可以将am ...

  9. Hello PyQt5

    在 ubuntu 系统上 GUI 编程,PyQt5 是个不错的选择.首先,当然是安装 PyQt5 了.终端输入命令: pip3 install PyQt5 即可. 1. 建立一目录 x01.PyQtH ...

  10. 原型模式和基于原型继承的js对象系统

    像同样基于原型编程的Io语言一样,javascript在原型继承方面,实现原理和Io非常类似,javascript也遵守这些原则 所有数据都是对象 要得到一个对象,不是通过实例化类,而是找到一个对象作 ...