Solution by <644367822>


  • 0
    6

    Approach #1 Brute Force [Accepted]

    Algorithm

    There are two situations about adjacent point

    1. point[i+1] - point [i] <= duration (time of poisoned condition) .

    2. point[i+1] - point [i] > duration .

    every point will refresh the time of poisoned condition.

    c++

    class Solution {
    public:
        int findPoisonedDuration(vector<int>& timeSeries, int duration) {
            if( timeSeries.size() == 0 ) return 0;
            int ans = 0 ;
            for ( int i = 0 ; i < timeSeries.size() - 1  ; i ++ ) {
                if(  timeSeries[i+1] - timeSeries[i] <= duration ) {
                    ans += timeSeries[i+1] - timeSeries[i] ;
                }
                else {
                    ans += duration ;
                }
            }
            ans += duration ;
            return ans ;
        }
    };
    

    Complexity Analysis

    • Time complexity : $O(n)$.
    • Space complexity: $O(1)$.

Log in to reply
 

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