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 的位置減, 然后將數組掃一遍逐個往后累加,即可得到正確結果。 想出這個方式的人好牛。