C++ O(n) Solution


  • 0
    C
    int findPoisonedDuration(vector<int>& timeSeries, int duration) {
            int total = 0;
            int actual_end = INT_MIN;
            int will_end = 0;
            for(int p:timeSeries){
                if( p <= actual_end ){       //if it attacks when last attacking hasn't finished
                    will_end = p+duration-1; 
                    total += duration-(actual_end-p+1); //duration - overlap time 
                    actual_end = will_end;
                }else{                       
                    total += duration;
                    actual_end = p+duration-1;
                }
            }
            return total;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.