返回介绍

solution / 0400-0499 / 0495.Teemo Attacking / README_EN

发布于 2024-06-17 01:04:00 字数 4255 浏览 0 评论 0 收藏 0

495. Teemo Attacking

中文文档

Description

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_.

 

Example 1:

Input: timeSeries = [1,4], duration = 2
Output: 4
Explanation: Teemo's 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.

Example 2:

Input: timeSeries = [1,2], duration = 2
Output: 3
Explanation: Teemo's 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.

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
    ans = duration
    for a, b in pairwise(timeSeries):
      ans += min(duration, b - a)
    return ans
class Solution {
  public int findPoisonedDuration(int[] timeSeries, int duration) {
    int n = timeSeries.length;
    int ans = duration;
    for (int i = 1; i < n; ++i) {
      ans += Math.min(duration, timeSeries[i] - timeSeries[i - 1]);
    }
    return ans;
  }
}
class Solution {
public:
  int findPoisonedDuration(vector<int>& timeSeries, int duration) {
    int ans = duration;
    int n = timeSeries.size();
    for (int i = 1; i < n; ++i) {
      ans += min(duration, timeSeries[i] - timeSeries[i - 1]);
    }
    return ans;
  }
};
func findPoisonedDuration(timeSeries []int, duration int) (ans int) {
  ans = duration
  for i, x := range timeSeries[1:] {
    ans += min(duration, x-timeSeries[i])
  }
  return
}
function findPoisonedDuration(timeSeries: number[], duration: number): number {
  const n = timeSeries.length;
  let ans = duration;
  for (let i = 1; i < n; ++i) {
    ans += Math.min(duration, timeSeries[i] - timeSeries[i - 1]);
  }
  return ans;
}
public class Solution {
  public int FindPoisonedDuration(int[] timeSeries, int duration) {
    int ans = duration;
    int n = timeSeries.Length;
    for (int i = 1; i < n; ++i) {
      ans += Math.Min(duration, timeSeries[i] - timeSeries[i - 1]);
    }
    return ans;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文