博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 495. Teemo Attacking (提莫攻击)
阅读量:4882 次
发布时间:2019-06-11

本文共 2831 字,大约阅读时间需要 9 分钟。

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], 2Output: 4Explanation: 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], 2Output: 3Explanation: 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 并且维护这个范围

1 class Solution  2 { 3     public int findPoisonedDuration(int[] timeSeries, int duration)  4     { 5         int totalTime = 0; 6         if(timeSeries == null || timeSeries.length == 0) 7             return totalTime; 8         // set up initial range 9         int start = timeSeries[0];10         int end = start + duration;11         // iterate from 2nd 12         for(int i=1; i
end)15 totalTime += duration; // add duration16 else // overlapped17 totalTime += timeSeries[i] - start;18 19 start = timeSeries[i]; // update start and end20 end = start + duration;21 }22 // add last range23 totalTime += end - start;24 25 return totalTime;26 }27 }

参考资料:N/A

 

LeetCode 题目列表 - 

 

转载于:https://www.cnblogs.com/jimmycheng/p/7590151.html

你可能感兴趣的文章
关于Spring注解@Async引发其他注解失效
查看>>
关于学习的一些感悟
查看>>
算法提高 概率计算
查看>>
UVa 12716 - GCD XOR(筛法 + 找规律)
查看>>
Spring Cloud学习资料
查看>>
制作无广告启动盘
查看>>
python使用httplib2访问REST服务的例子
查看>>
经典代码(01)
查看>>
生成ico格式图标
查看>>
并查集hdu4424
查看>>
jdbc之分页查询
查看>>
sbrk and coreleft
查看>>
树型DP
查看>>
怎么在ubuntu上使用pidgin登陆QQ
查看>>
思维的惰性
查看>>
【Android】学习记录<1> -- 初识ffmpeg
查看>>
关于IAsyncResult接口的CompletedSynchronously属性
查看>>
编译原理——算符优先分析文法(附源代码)
查看>>
jboss的启动过程
查看>>
渲染部分
查看>>