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 ]

题目标签:Array 

  题目给了我们一个2d updates, 和一个 length, 让我们返回一个 size = length 的array, 是经过 updates 的范围加法改动过的。

  因为题目hint 说了要时间复杂度O(k+n)。所以我们不能遇到每一个update,都去array 里改动一次。

  先遍历updates, 对于每一个update,我们只需要 标记 范围开始的 的那个number 和 范围结束的那个 number 的后一个。这里相当于,给每一个update 都规定了一个范围,开始是加,结尾后一个数字是减。

  再遍历res array,设一个sum = 0, 对于每一个number, 把number 的值加入sum里, 再把number = sum。这里的意思就是遇到任何一个范围开始的时候,进行累加,因为我们只更改了开头和结尾,所以中间都是没更改过的值,或者是其他的范围开头结尾。累加的作用就是把中间没改过的number 都补上该有的值。

  举例来看一下:

  updates = [1,3,2] 和 [2,4,3],length = 5

  0 0 0 0 0

  先遍历updates, 把开头和结尾标记

  0 0 0 -2  index 1 = 2;index 3+1 = -2;

  0 2 0 -2  index 2  = 3;index 4+1 超出了范围,就不用处理。

  遍历res array,进行累加 sum += res[i], res[i] = sum

   2 3 0 -2  sum = 0+0

  0 3 0 -2  sum = 0+2

  0 2 0 -2  sum = 2+3

  0 2 5 -2  sum = 5+0

  0 2 5 5    sum = 5-2  

  可以看到,从第一个范围开头开始,sum 进行累加,并更新number,如果遇到另一个范围,继续累加,如果遇到任何一个范围结束,把那一个范围累加的值减去,这个范围的加法就结束了,继续其他的。

Java Solution:

Runtime beats 77.60%

完成日期:09/16/2017

关键词:Array

关键点:只需要标记范围开始,和结束的位置,之后进行累加

 class Solution
{
public int[] getModifiedArray(int length, int[][] updates)
{
int [] res = new int[length]; // iterate each operation
for(int[] update: updates)
{
int start = update[0];
int end = update[1];
int val = update[2]; // mark first element
res[start] += val;
// mark last element (end + 1)
if(end + 1 < length)
res[end + 1] -= val;
} int sum = 0;
for(int i=0; i<length; i++)
{
sum += res[i];
res[i] = sum;
} return res;
}
}

参考资料:

https://discuss.leetcode.com/topic/49691/java-o-k-n-time-complexity-solution

LeetCode 题目列表 - LeetCode Questions List

LeetCode 370. Range Addition (范围加法)$的更多相关文章

  1. [LeetCode] 370. Range Addition 范围相加

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

  2. [LeetCode] 598. Range Addition II 范围相加之二

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  3. LeetCode: 598 Range Addition II(easy)

    题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are r ...

  4. 【LeetCode】370. Range Addition 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...

  5. LeetCode 598. Range Addition II (范围加法之二)

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  6. 370. Range Addition

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

  7. [LeetCode] 598. Range Addition II_Easy tag: Math

    做个基本思路可以用 brute force, 但时间复杂度较高. 因为起始值都为0, 所以肯定是左上角的重合的最小的长方形就是结果, 所以我们求x, y 的最小值, 最后返回x*y. Code    ...

  8. 598. Range Addition II 矩阵的范围叠加

    [抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...

  9. [LeetCode] Range Addition 范围相加

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

随机推荐

  1. Vagrant下共享目录下静态文件(js/jpg/png等)修改完运行报错

    利用Vagrant部署开发环境,使用目录共享模式,在本地磁盘进行开发,通过虚拟机环境运行开发的页面. 接下来打开页面,看上去一切正常,接下来将发生一个神奇的事情,你修改一个css文件,在刷新浏览器,发 ...

  2. SSH第一篇【整合SSH步骤、OpenSessionInView】

    前言 到目前为止,Struts2.Hibernate.Spring框架都过了一遍了.也写过了Spring怎么与Struts2整合,Spring与Hibernate整合-本博文主要讲解SSH的整合 整合 ...

  3. 【Debian 8.8】Java 8 安装以及环境变量配置

    事实上可以分为简单的三个步骤: 下载 JDK 压缩包 解压压缩包 配置环境变量 需要注意的是: 所有命令默认在 root 权限下进行! 演示环境是 Debian 8.8 64位 (阿里云学生机) 1. ...

  4. cocos2dx 播放gif

    起因 或许有人会说,cocos2dx中直接帧动画就行了用什么GIF. 起因是为游戏内部要用到第三方平台的头像,而第三方平台的头像大多都是用到Gif,所以才会有了这个需求 过程 查了各种文档都没找到.但 ...

  5. OpenStack Ocata 超详细搭建文档

    前言 搭建前必须看我本文档搭建的是分布式O版openstack(controller+ N compute + 1 cinder)的文档.openstack版本为Ocata.搭建的时候,请严格按照文档 ...

  6. Java中迭代器Iterator的使用

    Java集合类中Map接口下的相关类并没有像Collection接口的相关类一样实现get()方法,因此在要实现遍历输出的场景中没法直接用get()方法来取得对象中的数据,但Java本身提供了另一种遍 ...

  7. 使用apache反向代理tomacat

    起源 在大部分的生产环境中,基本上使用的都是java程序,从而促进了各种应用程序中间件的产生,在这里大概有几种,tomcat作为最著名的开源servlet容器,jboss也是开源的,而且有管理界面,主 ...

  8. 树状数组初步_ZERO

    原博客:树状数组 1 一维树状数组 1 什么是树状数组        树状数组是一个查询和修改复杂度都为log(n)的数据结构,假设数组A[1..n],那么查询A[1]+-+A[n]的时,间是log级 ...

  9. WINDOWS XP中用命令行管理用户 net user命令

    net user <username> [password or *] [/add] [options] [/domain] net user <username] /delete ...

  10. 怎样使用自定义标签简化 js、css 引入?

    国庆将至,工作兴致全无,来总结点项目里平时不起眼干货. 前端引入 js .css 一般是这样: <script type="text/javascript" src=&quo ...