码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 495. Teemo Attacking


    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.

    这道题好像没啥技巧……就顺着思路写。遍历数组,如果当前数字的下一个比它加上duration大,那就能attack到duration那么长时间,否则就只attack到下一个数字-当前数字的时间。因为最后一次必定能attack到duration那么长时间,所以遍历到倒数第二个就行,最后无脑加上duration返回。

    Runtime: 4 ms, faster than 63.73% of Java online submissions for Teemo Attacking.

    Memory Usage: 54 MB, less than 65.24% of Java online submissions for Teemo Attacking.

    1. class Solution {
    2. public int findPoisonedDuration(int[] timeSeries, int duration) {
    3. int result = 0;
    4. int i = 0;
    5. while (i < timeSeries.length - 1) {
    6. int end = timeSeries[i] + duration;
    7. if (timeSeries[i + 1] > end) {
    8. result += duration;
    9. } else {
    10. result += (timeSeries[i + 1] - timeSeries[i]);
    11. }
    12. i++;
    13. }
    14. return result + duration;
    15. }
    16. }

    然后看了discussion发现有人提出可以采用merge interval的思想,用两个变量start和end来记录当前attack的开始和结束时间。如果当前数字比end大,说明此次attack结束,result就加上end - start的时间并更新start和end;否则说明此次attack还没结束,只需要更新end。贴一个别人写的代码,就懒的自己写了……Loading...

  • 相关阅读:
    Redis下载安装教程 (windows)
    论文阅读(一)城市干道分段绿波协调控制模型研究
    四甲基罗丹明TRITC/羧甲基荧光素6-FAM修饰PLGA纳米载体,TRITC-PLGA,6-PEG-FAM-PEG-PLGA
    Java基础---第九篇
    VUE3 Router学习 第二章 导航守卫(全局前置、后置守卫)、路由元信息(meta)、过渡动效、滚动行为(scrollBehavior)
    面向对象编程原则(08)——合成复用原则
    长沙门店选址调查如何做?
    给你一本武林秘籍,和KeeWiDB一起登顶高性能
    【微信小程序】如何获得自己当前的定位呢?本文利用逆地址解析、uni-app带你实现
    GIS实战应用案例100篇(十四)-ArcGIS属性连接和使用Excel的问题
  • 原文地址:https://blog.csdn.net/qq_37333947/article/details/127574361
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号