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 ...
随机推荐
- 数据挖掘算法(一)C4.5
统计了14天的气象数据D(指标包括outlook,temperature,humidity,windy),并已知这些天气是否打球(play).如果给出新一天的气象指标数据:sunny,cool,hig ...
- ngui的tween的tweenFactor属性
ngui的tween的tweenFactor属性 这个属性是用来记录动画运行的位置的.可以通过设置它来达到动画运行到一半从新设置从新开始
- Starting MySQL... ERROR! The server quit without updating PID file 解决办法
来源:http://blog.rekfan.com/articles/186.html 我使用了第4条解决了问题 1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的 ...
- hg0088新2网址
封装通俗的说,就是我有一些秘密不想让人知道,就通过私有化变量和私有化方法,这样外界就访问不到了.hg0088新2网址然后如果你有一些很想让大家知道的东西,你就可以通过this创建的属性看作是对象共有属 ...
- VPS/云主机 如何试用远程连接登录主机服务器_
1.windows主机如何远程登录 点本地电脑开始>运行(或者按"window+R")>输入mstsc点确定 弹出远程连接的框输入IP连接, 如果是VPS,直接输入I ...
- boost.asio与boost.log同时使用导致socket不能正常收发数据
现象: 1. 没有使用boost.log前能正常收发数据 2.加入boost.log后async_connect没有回调 fix过程: 1. gdb调试发现程序block在pthread_timed_ ...
- google书签找回
解决办法: 1.查找google文件夹,win7为例子:C:\Users\ZhangSan\AppData\Local\Google\Chrome\User Data 找到这个文件夹,ZhangSan ...
- 引用、引用和术语定义<abbr><acronym><address><bdo><blockquote><q><cite><dfn>
<abbr>-缩写 <acronym>-首字母缩写 <address>-地址 <bdo>-文字方向 <blockquote>-长的引用 & ...
- docker --命令
1.开启服务 sudo docker start 服务名 2.预览列出所有的容器 sudo docker ps -a 3.进入文件 cd 4.预览文件目录 ls 5.预览文件内容 more 6.拷贝文 ...
- 2013年度最强AngularJS资源合集
原文在这里 2013年度最强AngularJS资源合集 来看我的视频教程,国内第一款完整的AngularJS视频教程http://www.imooc.com/learn/156 司徒正美 写道 htt ...