Leetcode: 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 ]
Hint:
Time Complexity: O(N+K), Space: O(1)
- For each update operation, do you really need to update all elements between i and j?
- Update only the first and end element is sufficient.
- The optimal time complexity is O(k + n) and uses O(1) extra space.
public class Solution {
public int[] getModifiedArray(int length, int[][] updates) {
int[] res = new int[length];
for (int[] update : updates) {
res[update[0]] += update[2];
if (update[1]+1 < length) res[update[1]+1] -= update[2];
}
for (int i=1; i<length; i++) {
res[i] = res[i] + res[i-1];
}
return res;
}
}
Leetcode: Range Addition的更多相关文章
- [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 范围相加之二
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
- [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: 598 Range Addition II(easy)
题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are r ...
- 598. Range Addition II 矩阵的范围叠加
[抄题]: Given an m * n matrix M initialized with all 0's and several update operations. Operations are ...
- 【leetcode_easy】598. Range Addition II
problem 598. Range Addition II 题意: 第一感觉就是最小的行和列的乘积即是最后结果. class Solution { public: int maxCount(int ...
- LeetCode 598. Range Addition II (范围加法之二)
Given an m * n matrix M initialized with all 0's and several update operations. Operations are repre ...
随机推荐
- 朋友圈常见单页面触屏滑动上下翻屏功能jQuery实现
翻页插件:实现原理,用margin-top来控制页面容器位置来实现上下翻页.margin这属性很好用,可以用来制作侧栏动画滑出菜单(左菜单,右内容,控制两者的margin实现):或者head下滑菜单 ...
- ACM: NBUT 1107 盒子游戏 - 简单博弈
NBUT 1107 盒子游戏 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format: Practice Appoint ...
- NOIp #2009
http://files.cnblogs.com/files/radiumlrb/NOIP2009%E6%8F%90%E9%AB%98%E7%BB%84%E5%A4%8D%E8%B5%9B%E8%AF ...
- mysql错误汇总
1,mysql报Fatal error encountered during command execution的解决办法 连接字符串里加上 Allow User Variables=True 解决. ...
- How to parse HTML page data in Windows Phone
1. Navigate to page WebBrowser control browser.Navigate(new Uri("http://www.xxxx.com")); 2 ...
- MySQL支持的数据类型(1)( 整数,小点,位)
整数类型 字节 最小值 最大值 tinyint 1 有符号-128 无符号0 有符号127 无符号255 smallint 2 有符号-32768 无符号0 有符号32767 无符号65535 m ...
- Ubuntu系统的安装与使用 深度音乐播放器
1.添加深度音乐播放器的ppa源并更新源缓存 sudo add-apt-repository ppa:noobslab/deepin-sc sudo apt-get update 2. 安装需要的依 ...
- wifi 破解
基础的知识: ESSID :无线网络的名字 BSSID :是AP的mac地址 CH: 工作信道 AP :无线访问接入点 WEP WPA/WPA2 STATION :客户机mac DHCP ...
- javascript学习之时间组件
写了一个时间组件,哪里需要哪里调(菜鸟级别,大牛路过就Ok了): 先有一个HTML文件: <!doctype> <html> <head> <title> ...
- 野路子出身PowerShell 文件操作实用功能 MSSQL123
因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职, 索性就网上各种搜,各种Demo,各种修改,花了半天时间,最后还是拼凑出来能达到效果了. 本身对PowerShe ...