LeetCode 370. Range Addition (范围加法)$
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 (范围加法)$的更多相关文章
- [LeetCode] 370. Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- [LeetCode] 598. Range Addition II 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- LeetCode: 598 Range Addition II(easy)
题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are r ...
- 【LeetCode】370. Range Addition 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...
- LeetCode 598. Range Addition II (范围加法之二)
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- 370. Range Addition
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- [LeetCode] 598. Range Addition II_Easy tag: Math
做个基本思路可以用 brute force, 但时间复杂度较高. 因为起始值都为0, 所以肯定是左上角的重合的最小的长方形就是结果, 所以我们求x, y 的最小值, 最后返回x*y. Code ...
- 598. Range Addition II 矩阵的范围叠加
[抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...
- [LeetCode] Range Addition 范围相加
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
随机推荐
- (转载)Oracle12g安装图解与安装过程常见问题注意事项
首附转载地址:http://jingyan.baidu.com/article/f96699bbab21c0894e3c1bf8.html 首先,点击"setup",建议以管理员身 ...
- Shiro初识与总结
1.1简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序 ...
- ArrayList,LinkedListd等容器使用时注意点:
1.对这两个List(包括其他的类似容器),如果向里面加入一个元素(引用数据类型),那么这个List里面保存的是这个对象的引用: 如果想要避免这种现象可以这样:在加入新的元素时不直接压,将已有的对象复 ...
- webpack2系列step1
第一篇:HTML 本文将一步一步的介绍webpack2的配置,从最基础的一直到与node结合. 操作都一样: midir step1 && cd step1 npm init -y n ...
- bzoj4033(树上染色)
树上染色 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两 ...
- Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- [js高手之路] javascript面向对象写法与应用
一.什么是对象? 对象是n个属性和方法组成的集合,如js内置的document, Date, Regexp, Math等等 document就是有很多的属性和方法, 如:getElementById, ...
- java人员正确使用IDEA 的方式
博主是Java开发人员,以前一直都用myeclipse来开发的,说实话感觉myeclipse毫无美感可言,后来经过同事介绍,认识了IDEA,一眼就相中了IDEA黑色的主题风格,自此就抛弃了旧爱myec ...
- JS表单提交的几种方式
第一种方式 : 表单提交,在 form 标签中增加 onsubmit 事件来判断表单是否提交成功 <script type="text/javascript"> fun ...
- java递归的应用和实例
使用计算机计算组合数: 1.使用组合数公式利用n!来计算 设计思想 (1)首先解决求n!的函数 (2)再结合组合数公式,求组合数 程序流程图 源程序代码 package Zuote; import j ...