美文网首页
370. Range Addition

370. Range Addition

作者: 我是你的果果呀 | 来源:发表于2016-12-06 09:51 被阅读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 的位置减, 然后将数组扫一遍逐个往后累加,即可得到正确结果。 想出这个方式的人好牛。

相关文章

网友评论

      本文标题:370. Range Addition

      本文链接:https://www.haomeiwen.com/subject/oralmttx.html