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 并且维护这个范围
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<timeSeries.length; i++) 13 { // not overlapped 14 if(timeSeries[i] > end) 15 totalTime += duration; // add duration 16 else // overlapped 17 totalTime += timeSeries[i] - start; 18 19 start = timeSeries[i]; // update start and end 20 end = start + duration; 21 } 22 // add last range 23 totalTime += end - start; 24 25 return totalTime; 26 } 27 }
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具