当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
Fabric. JS iText sets the color and background color of the specified text
idea開發工具常用的插件合集匯總
Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import
Reading notes of cgnf: conditional graph neural fields
文件包含漏洞(二)
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
Grbl software: basic knowledge of simple explanation
软件测试答疑篇
Alibaba: open source and self-developed liquid cooling data center technology
RGB infinite cube (advanced version)
随机推荐
1037 Magic Coupon
3D 打印机 G 代码命令:完整列表和教程
all3dp.com网站中全部Arduino项目(2022.7.1)
软件测试基础篇
RGB 无限立方体(高级版)
Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
软件测试 - 概念篇
Financial portal related information
文件包含漏洞(二)
Small and medium-sized projects to achieve certification and authorization of hand filter
1035 Password
Basic use of form
Sliding window on the learning road
Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import
Centos8 installation mysql8.0.22 tutorial
php继承(extends)
软件测试答疑篇
线程池概述
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond
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