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 的位置减, 然后将数组扫一遍逐个往后累加,即可得到正确结果。 想出这个方式的人好牛。
网友评论