Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)
Web TippsUse custom web fonts in Google Sheets charts(08.09.2026 um 17:05 Uhr)
Web TippsIntroducing the new 1Password App for Google Chat(08.09.2026 um 18:02 Uhr)

🔧 Programmierung 🕛 vor 2 Monaten 3 Min Lesezeit
0

Find Poisoned Duration (LeetCode 495) - Understanding Overlapping Intervals

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Difficulty: Easy

Topics: Array

Platform: Leetcode






Problem Statement



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.






Problem Statement Simplified



Imagine a character gets attacked multiple times, and each attack causes a poison effect that lasts for a fixed duration.

You are given:

timeSeries → the times when attacks happen.

duration → how long each poison effect lasts.



Your task is to calculate the total time the character remains poisoned.






Mistakes and Learning




  1. Overwriting the Result Instead of Accumulating




  • A common mistake is:


  • result = timeSeries.length * duration - overlap;


  • inside a loop.


  • This overwrites the previous value of result during every iteration.


  • Assuming Only One Overlap Exists


  • Another mistake is calculating overlap only between the current pair and directly subtracting it from the total duration.


  • Each pair of attacks can create a different overlap, so we need to process every attack individually.







Example 1






CODE
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






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









Key Insight




  1. Count Only the New Poison Time Added
    For every attack:




  • If the next attack occurs after the poison expires, add the full duration.


  • If the next attack occurs before the poison expires, add only the non-overlapping part.







Algorithm




  1.  Start with the first attack's poison durationSort the new array.

  2. Traverse the remaining attacksthe second largest

  3. Return the result






Algorithm in simple words



Think of poison as a timeline.

Every attack tries to add duration seconds of poison.

If there is no overlap, add the full duration.

If there is overlap, add only the time gap between attacks.



Repeat this for every attack and keep a running total.






Java code






CODE
class Solution {
public int findPoisonedDuration(int[] timeSeries, int duration) {
int result = duration;
if(timeSeries.length==1){
return duration;
}
else{

for (int i = 1; i < timeSeries.length; i++) {
result += Math.min(duration, timeSeries[i] - timeSeries[i-1]);
}

}
return result;
}
}










Time & Space Complexity



Time Complexity: O(n)

Space Complexity: O(1)

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
Use custom web fonts in Google Sheets charts
2 Quellen
Introducing the new 1Password App for Google Chat
1 Quelle
Context-aware access controls are available for Gemini Enterprise in the Admin console
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Find Poisoned Duration (LeetCode 495) - Understanding Overlapping Intervals

Thematisch verwandte Begriffe: Find, Poisoned, Duration, LeetCode · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...