All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0401_0500.s0495_teemo_attacking.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0401_0500.s0495_teemo_attacking;

// #Easy #Array #Simulation #2022_07_21_Time_2_ms_(97.97%)_Space_55_MB_(14.20%)

/**
 * 495 - Teemo Attacking\.
 *
 * Easy
 *
 * 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.
**/
public class Solution {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        if (duration == 0) {
            return 0;
        }
        int start = timeSeries[0];
        int end = timeSeries[0] + duration - 1;
        int poisonDuration = end - start + 1;
        for (int i = 1; i < timeSeries.length; i++) {
            if (timeSeries[i] <= end) {
                poisonDuration += (duration - (end - timeSeries[i] + 1));
                end += (duration - (end - timeSeries[i] + 1));
            } else {
                start = timeSeries[i];
                end = timeSeries[i] + duration - 1;
                poisonDuration += end - start + 1;
            }
        }
        return poisonDuration;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy