370. Range Addition

2016-12-06  本文已影响0人  我是你的果果呀

Assume you have an array of lengthninitialized with all0's and are givenkupdate operations.
Each operation is represented as a triplet:[startIndex, endIndex, inc]which increments each element of subarrayA[startIndex ... endIndex](startIndex and endIndex inclusive) withinc.
Return the modified array after allkoperations were executed.
Example:
Given:length = 5,
updates = [
[1,  3,  2],
[2,  4,  3],
[0,  2, -2]
]

Output: [-2, 0, 3, 5, 3]

常规方法大家都会,现在要求O(n+k) 的时间复杂度
start 位置+, end+1 的位置减, 然后将数组扫一遍逐个往后累加,即可得到正确结果。 想出这个方式的人好牛。

上一篇 下一篇

猜你喜欢

热点阅读