495.提莫偷袭(javascript)495.TeemoAttacking

原创
小哥 3年前 (2022-11-10) 阅读数 3 #大杂烩

在英雄联盟的世界里,有一个 “提莫” 英雄他的攻击可以使敌方英雄Ehi(编者注:冰射手)陷入中毒状态。

当蒂莫袭击艾许时,艾许的中毒状态仍在继续。 duration 秒。

正式地说,蒂莫在 t 发动攻击意味着Ehi处于时间间隔内。 [t, t + duration - 1](含 t 和 t + duration - 1)处于中毒状态。如果蒂莫对中毒的影响结束 前 再次攻击,中毒状态计时器将 重置 ,在新的攻击之后,中毒效应将在 duration 几秒钟后结束。

给你一个 非递减 整数数组 timeSeries ,其中 timeSeries[i] 表示Teemo在 timeSeries[i] 攻击Eish的时间为秒,表示中毒持续时间的整数。 duration 。

中毒归来艾Xi 总 秒数。

Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds. More formally, an attack at second t will mean Ashe is poisoned during the inclusive time interval [t, t + duration - 1]. If Teemo attacks again before the poison effect ends, the timer for it is reset, and the poison effect will end duration seconds after the new attack.

You are given a non-decreasing integer array timeSeries, where timeSeries[i] denotes that Teemo attacks Ashe at second timeSeries[i], and an integer duration.

Return the total number of seconds that Ashe is poisoned.

示例 1:

输入:timeSeries = [1,4], duration = 2
输出:4
说明:Teemo攻击对Eish的影响如下:
- 第 1 几秒钟后,蒂莫袭击了艾许,并使其立即中毒。将保持中毒状态 2 秒,即第 1 秒和第 2 秒。
- 第 4 几秒钟后,蒂莫再次袭击了艾许,艾许的中毒状态仍在继续。 2 秒,即第 4 秒和第 5 秒。
艾希在第 1、2、4、5 秒处于中毒状态,因此中毒秒的总数为 4 。

Example 1:

Input: timeSeries = [1,4], duration = 2
Output: 4
Explanation: Teemos attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 4, Teemo attacks, and Ashe is poisoned for seconds 4 and 5.
Ashe is poisoned for seconds 1, 2, 4, and 5, which is 4 seconds in total.

示例 2:

输入:timeSeries = [1,2], duration = 2
输出:3
说明:Teemo攻击对Eish的影响如下:
- 第 1 几秒钟后,蒂莫袭击了艾许,并使其立即中毒。将保持中毒状态 2 秒,即第 1 秒和第 2 秒。
- 第 2 几秒钟后,蒂莫再次攻击艾许,并重置了中毒计时器。艾许的中毒状态需要继续。 2 秒,即第 2 秒和第 3 秒。
艾希在第 1、2、3 秒处于中毒状态,因此中毒秒的总数为 3 。

Example 2:

Input: timeSeries = [1,2], duration = 2
Output: 3
Explanation: Teemos attacks on Ashe go as follows:
- At second 1, Teemo attacks, and Ashe is poisoned for seconds 1 and 2.
- At second 2 however, Teemo attacks again and resets the poison timer. Ashe is poisoned for seconds 2 and 3.
Ashe is poisoned for seconds 1, 2, and 3, which is 3 seconds in total.

提示:

1 <= timeSeries.length <= 104
0 <= timeSeries[i], duration <= 107
timeSeries 按 非递减 顺序排列

Constraints:

1 <= timeSeries.length <= 104
0 <= timeSeries[i], duration <= 107
timeSeries is sorted in non-decreasing order.

对timeSeries要遍历的阵列,

  1. 距离上次攻击时间的距离<中毒持续时间,中毒时间为timeSeries[i] - timeSeries[i - 1];
  2. 距离上次攻击时间的距离>=中毒持续时间,中毒时间为中毒持续时间

    /**

    • @param {number[]} timeSeries
    • @param {number} duration
    • @return {number} */ var findPoisonedDuration = function (timeSeries, duration) { let num = 0 let len = timeSeries.length for (let i = 1; i < len; i++) { let difference = timeSeries[i] - timeSeries[i - 1] if (difference < duration) { num += difference } else { num += duration } } return num + duration };

leetcode: https://leetcode.cn/problems/teemo-attacking/

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除