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 ...
随机推荐
- python编程(一)汉诺塔
题目描述 编写move(n, a, b, c)函数,它接收参数n,表示3个柱子A.B.C中第1个柱子A的盘子数量,然后打印出把所有盘子从A借助B移动到C的方法. 例: move(3, 'A', 'B' ...
- mac命令学习记录
1. 查找程序运行路径: which xxxx 2. 查找文件安装路径: whereis xxxx; 3. 编辑配置文件:vi ./.xxx : 进入需要编辑的文件: i 进行编辑 :输入:wq ...
- (42) Aeroo 模板实战
用writer设计一个采购单的模板 我用的是libreoffice 5.2.x 对于这个表格是通过工具栏上的插入指定的表格行和列完成,然后排版 对于单号po00001 这这样插入的 这样就完成一个订单 ...
- Unity3D Layout 快捷键
我的需求是开发的时候一种布局,运行的时候一种布局,Unity3D 选项中的自定义快捷键的太少,只能另想办法.Google 之后,找到解决方法:Editor layout hotkeys? 1.创建菜单 ...
- IE兼容问题,各类css hack代码(亲测有效)
现在大部分企业对浏览器兼容要求是IE7+或者IE8+,要求IE6的很少,此处一并写出. IE6: _margin-top: 20px; IE6+IE7: *margin-top: 20px; +mar ...
- 关于Cookies与Session系列一
这两个东西,最近项目操作的比较少,不过这两个在Web项目开发中一直都扮演着很重要的角色,有时有些细节会不小心就遗忘掉. Cookies 的概述 Cookies是由服务器端生成,发送给客户端,用来保存 ...
- 用 QGIS 画矢量交通路线图
一.准备工作 1.安装插件 为了方便画图,我们安装了OpenLayers,QuickOSM两个插件. 如何安装插件,度娘上都有答案.下图中打勾的部分为安装好的插件: OpenLayers提供了一些开放 ...
- hdoj 5074
Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan and C ...
- HDU--1232--畅通工程--并查集
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Bootstrap <基础二十五>警告(Alerts)
警告(Alerts)以及 Bootstrap 所提供的用于警告的 class.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添加一个 ...