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閱讀 398評論 0 0
  • 太陽在翻滾的云層中試圖沖破束縛。它用自己的光化作熾熱的箭,射向了周遭綿密悠遠的靶。 云中飽和著光和影,天空再也不是...
    不彥閱讀 503評論 2 3
  • 高看了自己 也高看了你 我太認死理 你太會承諾 我太放不下 你太不在乎 也許 我太閑了 而你 偏偏很忙
    瑜魚_yu閱讀 132評論 0 0
  • 兩手空空。
    Molly小姐不說謊閱讀 106評論 0 0