leetcode 370 range addtion

原题是:

Assume you have an array of length n initialized with all 0's and are given k update operations.

Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc.

Return the modified array after all k operations were executed.

Example:

Given:

length = 5,
updates = [
    [1,  3,  2],
    [2,  4,  3],
    [0,  2, -2]
]

Output:

[-2, 0, 3, 5, 3]

Explanation:

Initial state:
[ 0, 0, 0, 0, 0 ]

After applying operation [1, 3, 2]:
[ 0, 2, 2, 2, 0 ]

After applying operation [2, 4, 3]:
[ 0, 2, 5, 5, 3 ]

After applying operation [0, 2, -2]:
[-2, 0, 3, 5, 3 ]

思路1 :(超时)

Screen Shot 2017-10-09 at 10.20.26 PM.png

思路2:(超空间)

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

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,351评论 0 33
  • 梦见了 怕幡醒 梦断了 难续上 甚思念 惧相见 怕见了 梦碎了
    简一的小時光阅读 1,937评论 0 1
  • 二十日 火车呼啸着在深黑的迷雾中疾驰。路灯在雾中洒下一段段光柱,匆匆地从窗前闪过。明早天一亮,火车就会路过长沙。中...
    水如歌阅读 1,384评论 2 0
  • 看到了两部主线雷同的剧:《回家的诱惑》和《我的前半生》。 家庭主妇、第三者、家庭主妇华丽升级或腹黑。三点一线。前半...
    露拉阅读 1,722评论 0 0