惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
D
Docker
J
Java Code Geeks
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
The GitHub Blog
The GitHub Blog
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
腾讯CDC
罗磊的独立博客
U
Unit 42
爱范儿
爱范儿
Vercel News
Vercel News
MyScale Blog
MyScale Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Find Poisoned Duration (LeetCode 495) - Understanding Ove...
Jerin · 2026-06-27 · via DEV Community
Cover image for Find Poisoned Duration (LeetCode 495) - Understanding Overlapping Intervals

Jerin

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

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.

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

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)