当前位置:网站首页>495.提莫攻击
495.提莫攻击
2022-07-02 05:50:00 【有时候。】
1. 题目描述
在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄。他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。
当提莫攻击艾希,艾希的中毒状态正好持续 duration 秒。
正式地讲,提莫在 t 发起发起攻击意味着艾希在时间区间 [t, t + duration - 1](含 t 和 t + duration - 1)处于中毒状态。如果提莫在中毒影响结束 前 再次攻击,中毒状态计时器将会 重置 ,在新的攻击之后,中毒影响将会在 duration 秒后结束。
给你一个 非递减 的整数数组 timeSeries ,其中 timeSeries[i] 表示提莫在 timeSeries[i] 秒时对艾希发起攻击,以及一个表示中毒持续时间的整数 duration 。
返回艾希处于中毒状态的总秒数。
输入:timeSeries = [1,4], duration = 2
输出:4
解释:提莫攻击对艾希的影响如下:
- 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
- 第 4 秒,提莫再次攻击艾希,艾希中毒状态又持续 2 秒,即第 4 秒和第 5 秒。
艾希在第 1、2、4、5 秒处于中毒状态,所以总中毒秒数是 4
2. 解题思路
① 总中毒秒数至少为duration秒,因为timeSeries的最后一个时刻艾希总能中毒duration秒
② 遍历timeSeries,判断timeSeries中相邻时刻的间隔是否大于duration,若大于或者等于,则中毒秒数增加duration,若小于,增加间隔即可。
- python3
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
res = duration
if duration == 0:
return 0
for i in range(1,len(timeSeries)):
if timeSeries[i] - timeSeries[i-1] >= duration:
res += duration
else:
res += timeSeries[i] - timeSeries[i-1]
return res
- c++
class Solution {
public:
int findPoisonedDuration(vector<int>& timeSeries, int duration) {
int res = duration;
if (duration == 0)
return 0;
for (int i=1; i < timeSeries.size(); i++){
if (timeSeries[i]-timeSeries[i-1] >= duration)
res += duration;
else
res += timeSeries[i] - timeSeries[i-1];
}
return res;
}
};
边栏推荐
- How to change the IP address of computer mobile phone simulator
- php获取cpu使用率、硬盘使用、内存使用
- c语言中的几个关键字
- Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
- Practice C language advanced address book design
- How to write good code - Defensive Programming Guide
- 死磕大屏UI,FineReport开发日记
- idea开发工具常用的插件合集汇总
- "Simple" infinite magic cube
- Lantern Festival gift - plant vs zombie game (realized by Matlab)
猜你喜欢
如何写出好代码 — 防御式编程指南
Lantern Festival gift - plant vs zombie game (realized by Matlab)
Centos8 installation mysql8.0.22 tutorial
Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
3D printer G code command: complete list and tutorial
JVM class loading mechanism
Fabric. JS iText superscript and subscript
A collection of commonly used plug-ins for idea development tools
2022-2-14 learning xiangniuke project - Section 6 displays login information
随机推荐
Lantern Festival gift - plant vs zombie game (realized by Matlab)
3D printer G code command: complete list and tutorial
php父类(parent)
The Hong Kong Stock Exchange learned from US stocks and pushed spac: the follow-up of many PE companies could not hide the embarrassment of the world's worst stock market
[technical notes-08]
Zzuli:1062 greatest common divisor
Technologists talk about open source: This is not just using love to generate electricity
【技术随记-08】
Youth training camp -- database operation project
Visual Studio导入
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
TypeScript的泛型和泛型约束
【LeetCode】Day92-盛最多水的容器
Common protocols and download paths of NR
Financial portal related information
线程池概述
记录sentry的踩坑之路
Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
Software testing Q & A
Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders