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的更多相关文章

  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】370. Range Addition 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 只修改区间起终点 日期 题目地址:https://le ...

  4. 598. Range Addition II 矩阵的范围叠加

    [抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...

  5. [LeetCode] Range Addition 范围相加

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

  6. [LeetCode] Range Addition II 范围相加之二

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

  7. LeetCode Range Addition II

    原题链接在这里:https://leetcode.com/problems/range-addition-ii/description/ 题目: Given an m * n matrix M ini ...

  8. LeetCode: 598 Range Addition II(easy)

    题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are r ...

  9. [LeetCode] 598. Range Addition II 范围相加之二

    Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...

随机推荐

  1. text/plain && text/html

    text/plain和text/html都是Content-Type; text/plain : 页面以文本形式输出 text/html:  页面以html格式输出

  2. python——django入门篇

    要做一只有自学能力的pythoner,尽管大多数自学都是野生并不规范的,会遇到诸多坑,最后用稀奇古怪的方法解决了,但是先了解一些为以后真正学习道路填坑方便了简直不只一点点...重点来了:感觉以班里同学 ...

  3. 比CMD更强大的命令行WMIC

    先决条件:a. 启动Windows Management Instrumentation服务,开放TCP135端口.b. 本地安全策略的“网络访问: 本地帐户的共享和安全模式”应设为“经典-本地用户以 ...

  4. cocos2dx 3.1获取系统当前时间

    std::string Tools::getcurrTime() { #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATF ...

  5. Dns 类

    Dns 类型公开以下成员. 方法       名称 说明 BeginGetHostAddresses 异步返回指定主机的 Internet 协议 (IP) 地址. BeginGetHostByName ...

  6. IIS部署WCF网站服务步骤

    一开始在“管理工具”下找不到IIS的快捷方式,是因为系统默认未打开IIS功能,所以首先打开IIS功能: 为避免出现未知的麻烦,建议将IIS下的所有项都勾上: 创建IIS桌面快捷方式: 添加网站托管: ...

  7. 课堂作业二 PAT1025 反转链表

    MyGitHub 终于~奔溃了无数次后,看到这个结果 ,感动得不要不要的::>_<:: 题目在这里 题目简述:该题可大致分为 输入链表 -> 链表节点反转 -> 两个步骤 输入 ...

  8. mydumper linux mysql 备份利器

    1 官网 https://launchpad.net/ 2 安装使用参考网站   http://www.cnblogs.com/digdeep/p/4925560.html

  9. navicat 创建的表,username字段不能接受中文名字。

    用navicat 创建的表,username(varchar)字段不能接受中文名字. 解决方法如下: 选中所创建的users表,打开“设计表”,选中username字段,看到下面是字符集latin1, ...

  10. Spring Framework的核心:IOC容器的实现

    2.1   Spring IoC容器概述 2.1.1 IoC容器和依赖反转模式 依赖反转的要义,如果合作对象的引用或依赖关系的管理由具体对象来完成,会导致代码的高度耦合和可测性的降低.依赖控制反转的实 ...