leetcode-495. 提莫攻击

题目

495. 提莫攻击

解法

返回中毒秒数

class Solution {
    
    /**
     * @param Integer[] $timeSeries
     * @param Integer $duration
     * @return Integer
     */
    function findPoisonedDuration($timeSeries, $duration) {
        if (empty($timeSeries) || empty($duration)) {
            return 0;
        }
        
        $len = count($timeSeries);
        $ret = 0;
        foreach ($timeSeries as $key => $point) {
            if (!isset($timeSeries[$key+1])) {
                $ret += $duration;
                continue;
            }
            
            if ($point + $duration - 1 >= $timeSeries[$key+1]) {
                $ret += $timeSeries[$key+1] - $point;
            } else {
                $ret += $duration;
            }
        }
        
        return $ret;
    }
}

写完之后,默默打开了 lolm

posted @ 2022-01-02 22:40  吴丹阳-V  阅读(27)  评论(0编辑  收藏  举报