C++ Straight forward solution Time/Space: O(n)/O(1)


  • 0
    C
    class Solution {
    public:
        int findPosisonedDuration(vector<int>& timeSeries, int duration) {
            int result = 0;
            for(int i = 0; i < timeSeries.size(); ++i) {
                // Last ellement OR next element is within the duration of this attack
                if(i+1 == timeSeries.size() || timeSeries[i] + duration <= timeSeries[i+1] ) {
                    result += duration;
                }
                else {
                    result += timeSeries[i+1] - timeSeries[i];
                }
            }
            return result;
        }
    };
    

Log in to reply
 

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