Assume you have an array of length n initialized with all 0’s and are given k update operations.
Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex … endIndex] (startIndex and endIndex inclusive) with inc.
Return the modified array after all k operations were executed.
Example:
Explanation:
解法1: O(n) + O(k)
这题如果用一般的办法会TLE。关键的点在于不用更新start和end之间所有的数。那么我们只更新start的数,意思是从这往后所有的数都要加上start对应的数字。同时需要在end+1的地方减去increment,意思是从这里往后所有的数要减去start对应的数字,以此来抵消多加的那一份。
最后我们在遍历一遍数组,做一个累加就得到了结果。
C++
Java