• python 练习题495. 提莫攻击


    地址:https://leetcode-cn.com/problems/teemo-attacking/

     1 '''
     2 在《英雄联盟》的世界中,有一个叫 “提莫” 的英雄。他的攻击可以让敌方英雄艾希(编者注:寒冰射手)进入中毒状态。
     3 
     4 当提莫攻击艾希,艾希的中毒状态正好持续 duration 秒。
     5 
     6 正式地讲,提莫在 t 发起发起攻击意味着艾希在时间区间 [t, t + duration - 1](含 t 和 t + duration - 1)处于中毒状态。如果提莫在中毒影响结束 前 再次攻击,中毒状态计时器将会 重置 ,在新的攻击之后,中毒影响将会在 duration 秒后结束。
     7 
     8 给你一个 非递减 的整数数组 timeSeries ,其中 timeSeries[i] 表示提莫在 timeSeries[i] 秒时对艾希发起攻击,以及一个表示中毒持续时间的整数 duration 。
     9 
    10 返回艾希处于中毒状态的 总 秒数。
    11 
    12  
    13 示例 1:
    14 
    15 输入:timeSeries = [1,4], duration = 2
    16 输出:4
    17 解释:提莫攻击对艾希的影响如下:
    18 - 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
    19 - 第 4 秒,提莫再次攻击艾希,艾希中毒状态又持续 2 秒,即第 4 秒和第 5 秒。
    20 艾希在第 1、2、4、5 秒处于中毒状态,所以总中毒秒数是 4 。
    21 示例 2:
    22 
    23 输入:timeSeries = [1,2], duration = 2
    24 输出:3
    25 解释:提莫攻击对艾希的影响如下:
    26 - 第 1 秒,提莫攻击艾希并使其立即中毒。中毒状态会维持 2 秒,即第 1 秒和第 2 秒。
    27 - 第 2 秒,提莫再次攻击艾希,并重置中毒计时器,艾希中毒状态需要持续 2 秒,即第 2 秒和第 3 秒。
    28 艾希在第 1、2、3 秒处于中毒状态,所以总中毒秒数是 3 。
    29  
    30 
    31 提示:
    32 
    33 1 <= timeSeries.length <= 104
    34 0 <= timeSeries[i], duration <= 107
    35 timeSeries 按 非递减 顺序排列
    36 
    37 
    38 
    39 '''
    40 
    41 
    42 
    43 class Solution:
    44     def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
    45         total = duration
    46         l = len(timeSeries)
    47         if l==1:return duration
    48         for i in range(1,l):
    49             timeDura = timeSeries[i] - timeSeries[i-1]
    50             if  timeDura>= duration:
    51                 total += duration
    52             else: 
    53                 total += timeDura
    54         return total

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/teemo-attacking

  • 相关阅读:
    Airflow 使用 Celery 时,如何添加 Celery 配置
    什么是唯品会JIT业务
    Linux 性能优化排查工具
    HttpClient 报错 Invalid cookie header, Invalid 'expires' attribute: Thu, 01 Jan 1970 00:00:00 GMT
    如何使用 Enterprise Architect 画 UML
    通过maven profile 打包指定环境配置
    Git 使用总结
    Git 分支模型
    本地Windows环境Dubbo搭建测试
    makefile
  • 原文地址:https://www.cnblogs.com/whycai/p/15535715.html
Copyright © 2020-2023  润新知