题目地址:https://leetcode-cn.com/problems/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:

Input: 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]

题目大意

假设你有一个长度为 n 的数组,初始情况下所有的数字均为 0,你将会被给出 k​个更新的操作。
其中,每个操作会被表示为一个三元组:[startIndex, endIndex, inc],你需要将子数组 A[startIndex … endIndex](包括 startIndex 和 endIndex)增加 inc。
请你返回 k 次操作后的数组。

解题方法

只修改区间起终点

我第一次做的时候,把[start,end]区间内的所有元素进行了遍历修改,会导致超时。

看了官方解答之后明白,哦,原来只用修改起始位置和结束位置就行了,让区间起点+=inc,区间终点-=inc,区间中间的部分暂时不用更新。最后从左到右再遍历一次,累计求和并修改每个位置的值。

总的时间复杂度是O(N + k),空间复杂度是O(1).

C++代码如下:

class Solution {
public:
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
vector<int> res(length, 0);
for (auto& up : updates) {
int start = up[0], end = up[1], inc = up[2];
res[start] += inc;
if (end < length - 1)
res[end + 1] -= inc;
}
int cursum = 0;
for (int i = 0; i < length; ++i) {
cursum += res[i];
res[i] = cursum;
}
return res;
}
};

参考资料:https://leetcode-cn.com/problems/range-addition/solution/qu-jian-jia-fa-by-leetcode/

日期

2019 年 9 月 18 日 —— 今日又是九一八

【LeetCode】370. Range Addition 解题报告(C++)的更多相关文章

  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 370. Range Addition (范围加法)$

    Assume you have an array of length n initialized with all 0's and are given k update operations. Eac ...

  3. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  6. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  7. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  8. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  9. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

随机推荐

  1. 18-Rotate Array-Leetcode

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...

  2. 通过yum安装 memcache

    . 通过yum安装 复制代码代码如下: yum -y install memcached#安装完成后执行:memcached -h#出现memcached帮助信息说明安装成功 2. 加入启动服务 复制 ...

  3. 求解线性递推方程第n项的一般方法

    概述 系数为常数,递推项系数均为一次的,形如下面形式的递推式,称为线性递推方程. \[f[n]=\begin{cases} C &n\in Value\\ a_1 f[n-1]+a_2 f[n ...

  4. Kubernetes主机间cluster ip时通时不通

    1.问题现象 测试部署了一个service,包括2个pod,分别在node1和node2上. $ kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) ...

  5. Hadoop fs.copyToLocalFile错误

    fs.copyToLocalFile(new Path("/study1/1.txt"), new Path("C:/Users/Administrator/Deskto ...

  6. MVC、MVVM模式

    MVC 上个世纪70年代,美国施乐帕克研究中心,就是那个发明图形用户界面(GUI)的公司,开发了Smalltalk编程语言,并开始用它编写图形界面的应用程序. 到了Smalltalk-80这个版本的时 ...

  7. 2021广东工业大学十月月赛 F-hnjhd爱序列

    题目:GDUTOJ | hnjhd爱序列 (gdutcode.cn) 一开始是用双指针从尾至头遍历,但发现会tle!! 后来朋友@77给出了一种用桶的做法,相当于是用空间换时间了. 其中用到的一个原理 ...

  8. 虚机扩大容量与vm减少所占容量

    Linux的虚拟机碎片整理 sudo dd if=/dev/zero of=/free bs=1M sudo rm -f /free 镜像压缩 移动镜像 VBoxManage internalcomm ...

  9. Mysql的索引调优详解:如何去创建索引以及避免索引失效

    在正式介绍Mysql调优之前,先补充mysql的两种引擎 mysql逻辑分层 InnoDB:事务优先(适合高并发操作,行锁) MyISAM:性能优先(表锁) 查看使用的引擎: show variabl ...

  10. LoadRunner中怎么设置密码参数化与用户名关联

    对密码参数化时从parameter里的"Select next row"列表中选择Same Line As这一选项,意思就是每一个密码参数化取值与对应行的用户名关联起来了