LeetCode 495. Teemo Attacking (提莫攻击)
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:
- You may assume the length of given time series array won't exceed 10000.
- 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 (提莫攻击)的更多相关文章
- [LeetCode] 495. Teemo Attacking 提莫攻击
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- 495 Teemo Attacking 提莫攻击
在<英雄联盟>的世界中,有一个叫“提莫”的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和提莫攻击的中毒持续时间,你需要输出艾希的中毒 ...
- [LeetCode] Teemo Attacking 提莫攻击
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- 【LeetCode】495. Teemo Attacking 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 495. Teemo Attacking
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- [Swift]LeetCode495. 提莫攻击 | Teemo Attacking
In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned ...
- Java实现 LeetCode 495 提莫攻击
495. 提莫攻击 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和 ...
- [Leetcode]495.提莫攻击
题目: 在<英雄联盟>的世界中,有一个叫 "提莫" 的英雄,他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态.现在,给出提莫对艾希的攻击时间序列和提莫攻击的中 ...
- leetcode解题报告(32):Teemo Attacking
描述 In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poison ...
随机推荐
- Java:接口继承接口 (多继承机制)
在看API文档时,突然发现一个接口可以是其他接口的子接口,这说明接口之间会存在继承的关系.查找了相关的资料,做一个总结. 是继承还是实现 首先要搞清楚接口之间的关系使用的关键字是extends还是im ...
- 随便讲讲我对于svn和git的想法
1.SVN是集中式版本管理工具,而Git是分布式版本管理工具,这是核心区别. 二者都有集中的库,只是git偏向于分布式,用户可以再自己电脑上克隆一份自己的库,即使在断网的情况下也能够查看版本,创建分支 ...
- ①【javascript设计到的技术点】
一.dom操作: document.getElementById() document.getElementsByTagName() 二.事件操作: dom2级事件 主流浏览器 addEventLis ...
- 内核对象 windows操作系统
问题: 什么是内核对象? 答:内核对象实际上时由内核分配的一块内存,而且只能由内核来访问.它时一个数据结构,成员包含有关于该对象的信息.一些成员对于所有对象类型都是一样的,比如对象名称.安全描述.使用 ...
- HashMap的源码分析(一)
1.hashMap的关键值 DEFAULT_INITIAL_CAPACITY:默认初始容量16,∈(0,1<<30),实际大小为2的整数次幂: DEFAULT_LOAD_FACTOR:默认 ...
- codeforces 8c Looking for Order
https://vjudge.net/problem/CodeForces-8C 题意: 一个平面上放着许多东西,每个东西都有一个坐标,最开始一个人在一个起始坐标,她出发去拿东西,一次要么拿一件东西, ...
- python之路第五篇之递归(进阶篇:续:经典例子剖析)
递归 在函数内部,可以调用其他函数; 如果一个函数在内部调用自身本身,这个函数就是递归函数. 例如,我们来计算阶乘: n! = 1 x 2 x 3 x ... x n, 用函数f1(n)表示,可以看出 ...
- 21.Linux-写USB键盘驱动(详解)
本节目的: 根据上节写的USB鼠标驱动,来依葫芦画瓢写出键盘驱动 1.首先我们通过上节的代码中修改,来打印下键盘驱动的数据到底是怎样的 先来回忆下,我们之前写的鼠标驱动的id_table是这样: 所以 ...
- 醒醒吧!互联网的真正未来不是AI,更不是VR,AR,而是区块链
这些力量并非命运,而是轨迹.他们提供的并不是我们将去向何方的预测,而是告诉我们,在不远的将来,我们会向那个方向前行,必然而然. ---凯文•凯利 文字与货币 人类在演化过程中,凭借智慧创造了无数事物, ...
- 【转】Python-__builtin__与__builtins__的区别与关系(超详细,经典)
在学习Python时,很多人会问到__builtin__.__builtins__和builtins之间有什么关系.百度或Google一下,有很 多答案,但是这些答案要么不准确,要么只说了一点点,并不 ...