370. Range Addition

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


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Assume you have an array of length n initialized with all...
    Jeanz阅读 2,939评论 0 0
  • 太阳在翻滚的云层中试图冲破束缚。它用自己的光化作炽热的箭,射向了周遭绵密悠远的靶。 云中饱和着光和影,天空再也不是...
    不彦阅读 3,388评论 2 3
  • 高看了自己 也高看了你 我太认死理 你太会承诺 我太放不下 你太不在乎 也许 我太闲了 而你 偏偏很忙
    瑜鱼_yu阅读 871评论 0 0
  • 两手空空。
    Molly小姐不说谎阅读 715评论 0 0