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. 私人订制——属于你自己的Linux

    一.前言 Linux操作系统至1991年10月5日诞生以来,就其开源性和自由性得到了很多技术大牛的青睐,每个Linux爱好者都为其贡献了自己的一份力,不管是在Linux内核还是开源软件等方面,都为我们 ...

  2. 纳税服务系统【用户模块之使用POI导入excel、导出excel】

    前言 再次回到我们的用户模块上,我们发现还有两个功能没有完成: 对于将网页中的数据导入或导出到excel文件中,我们是完全没有学习过的.但是呢,在Java中操作excel是相对常用的,因此也有组件供我 ...

  3. [01] Java语言的基本认识

    0.写在前面的话 我们都知道在计算机的底层,它是识别二进制的,也就是说,计算机只能认识0和1.这主要是因为电路的逻辑只有两种状态,所以只需要0和1两个数字就可以表示低电平和高电平.而计算机是由数不清的 ...

  4. Hibernate中的主键生成器generator

    本文讲述Hibernate的generator属性的意义.Generator属性有7种class,本文简略描述了这7种class的意义和用法. [xhtml] view plaincopy <c ...

  5. Swiper.js使用方法

    <!DOCTYPE html> <html> <head>  ... <link rel="stylesheet" href=" ...

  6. Linux入门之常用命令(5)Bash

    bash优点 1.命令编辑能力  记录使用过的命令 ~/.bash_history 记录上次的历史命令 2.补全功能  指令补全两下tab 文件名补全 3.命令别名  alias [别名]=‘’ 4. ...

  7. http://codeforces.com/contest/536/problem/B

    B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. EnCase missed some usb activities in the evidence files

    My friend is a developer and her colleague May was suspected of stealing the source code of an impor ...

  9. SpringMVC的一点理解

    1.MVC(Model-View-Controller) 用慕课网上的一个图来看看MVC Front Controller(前端控制器):把客户的请求分发给不同的控制器去生成业务数据,将生成的业务数据 ...

  10. Python系列之内置函数

    内置函数 一.数学运算类: abs(a):求绝对值如果参数是个复数则返回复数的模. a = abs(-1) print(a) >>>1 compilex([real[, imag]] ...