博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Teemo Attacking 提莫攻击
阅读量:6682 次
发布时间:2019-06-25

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

In LLP 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.

LeetCode果然花样百出,连提莫都搬上题目了,那个草丛里乱种蘑菇的小提莫,那个“团战可以输提莫必须死”的提莫??可以,服了,坐等女枪女警轮子妈的题目了~好了,不闲扯了,其实这道题蛮简单的,感觉不能算一道medium的题,就直接使用贪心算法,比较相邻两个时间点的时间差,如果小于duration,就加上这个差,如果大于或等于,就加上duration即可,参见代码如下:

class Solution {public:    int findPoisonedDuration(vector
& timeSeries, int duration) { if (timeSeries.empty()) return 0; int res = 0, n = timeSeries.size(); for (int i = 1; i < n; ++i) { int diff = timeSeries[i] - timeSeries[i - 1]; res += (diff < duration) ? diff : duration; } return res + duration; }};

本文转自博客园Grandyang的博客,原文链接:,如需转载请自行联系原博主。

你可能感兴趣的文章
Error: "Call requires API level 11 (current min is 8): android.app.Activity#onCreateView"
查看>>
Ubuntu12.04下Linux内核编译
查看>>
Codeforces Round #113 (Div. 2) Tetrahedron(滚动DP)
查看>>
结构体的使用
查看>>
NOR flash and NAND flash
查看>>
关于异步委托的调用与应用场景
查看>>
flask+redis实现抢购(秒杀)功能
查看>>
Linux 学习笔记 (一)在VMware 中安装 Ubtuntu 以及 VMware tools
查看>>
经典测试用例
查看>>
3月16日学习内容整理:metaclass
查看>>
Vue和其他框架的区别
查看>>
【iPhone5概念机最新效果图曝光】
查看>>
深入浅出:5G和HTTP
查看>>
ES6-let和const命令
查看>>
java反射,代码优化
查看>>
python中获取当前所有的logger
查看>>
关于BDD100k数据输入处理mask变为56*56
查看>>
Reveal使用心法
查看>>
Directx11教程(18) D3D11管线(7)
查看>>
JVM系列二:GC策略&内存申请、对象衰老
查看>>