Short O(n) C++ solution


  • 6
    K
    lass Solution {
    public:
        int findPosisonedDuration(vector<int>& ts, int duration) {
            int tp = 0, pe = 0;
            for(int i=0; i<ts.size(); ++i){
                tp += duration - (ts[i] < pe) *(pe - ts[i]);
                pe = ts[i] + duration;
            }
            return tp;
        }
    };

  • 0
    L

    @kevin36 oh! interesting method ! The opposite logic.


Log in to reply
 

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