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 ]
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
vector<int> ret(length, );
for (int i = ; i < updates.size(); i++) {
ret[updates[i][]] += updates[i][];
int endIdx = updates[i][] + ;
if (endIdx < length)
ret[endIdx] -= updates[i][];
}
for (int i = ; i < length; i++) {
ret[i] += ret[i - ];
}
return ret;
}
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 370. Range Addition (范围加法)$
Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...
- 【LeetCode】370. Range Addition 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...
- 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 ...
- [LeetCode] Range Addition II 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- LeetCode Range Addition II
原题链接在这里:https://leetcode.com/problems/range-addition-ii/description/ 题目: Given an m * n matrix M ini ...
- 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] 598. Range Addition II 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
随机推荐
- java, listmap2json, fastjson
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;impor ...
- MySQL exists的用法介绍
有一个查询如下: 1 SELECT c.CustomerId, CompanyName 2 FROM Customers c 3 WHERE EXISTS( 4 SELECT Or ...
- YY前端课程2
1. alt属性对SEO优化很重要 2. 最早的网页是靠table布局标签,后来用div+css进行网页重构(因此现在网页设计的名字由网页设计变成了网页重构) 3. 静态网页和后台没有交互 动态网页和 ...
- Asp.net MVC过滤器的使用
当我们网站开发到这里的时候,我们虽然已经实现了用户登录信息,用户不经过登录信息,比如:http://localhost:6941/UserInfo/Index如果我这样访问的话,他是可以进行操作的,所 ...
- <c:forEach>循环list,一个表格两列数据
参考: http://zhidao.baidu.com/link?url=apG5dUmW7RjB5eOYKSWOWdKd7nxFpkDO4n3i8R6MWYKl7E2JC1OCtPILF4G4EUO ...
- [原创]VM虚拟机Centos6.4网络配置。
关于虚拟机VMware 3种网络模式(桥接.nat.Host-only)的工作原理http://www.cnblogs.com/hehexiaoxia/p/4042583.html 操作环境 主机:W ...
- CodeForces #363 div2 Vacations DP
题目链接:C. Vacations 题意:现在有n天的假期,对于第i天有四种情况: 0 gym没开,contest没开 1 gym没开,contest开了 2 gym开了,contest没开 3 ...
- 2015年最好的PHP框架调查统计
2015年最流行的框架,Laravel框架获得赢家! 正如预期的那样,Laravel这次又一次高出了一大截. 有一些人可能会担心,部分框架分裂版本可能影响Laravel的调查结果,并给它一个不公平的优 ...
- pwnable.kr-flag
题目: 使用 file 命令查看下载回来的 flag 文件,发现是一个64位的 ELF 可执行程序.通过查看,发现其具有明显的 UPX 压缩标志,所以解压之. 使用命令upx –d 进行脱壳,如果没有 ...
- ros科大讯飞语音识别
转自http://www.ncnynl.com/archives/201611/1069.html ROS入门教程-编写科大讯飞语音SDK的ROS包 说明 ROS软件包xfei_asr是集成自科大讯飞 ...