LeetCode 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 ]


题目标签:Array 

  题目给了我们一个2d updates, 和一个 length, 让我们返回一个 size = length 的array, 是经过 updates 的范围加法改动过的。

  因为题目hint 说了要时间复杂度O(k+n)。所以我们不能遇到每一个update,都去array 里改动一次。

  先遍历updates, 对于每一个update,我们只需要 标记 范围开始的 的那个number 和 范围结束的那个 number 的后一个。这里相当于,给每一个update 都规定了一个范围,开始是加,结尾后一个数字是减。

  再遍历res array,设一个sum = 0, 对于每一个number, 把number 的值加入sum里, 再把number = sum。这里的意思就是遇到任何一个范围开始的时候,进行累加,因为我们只更改了开头和结尾,所以中间都是没更改过的值,或者是其他的范围开头结尾。累加的作用就是把中间没改过的number 都补上该有的值。

  举例来看一下:

  updates = [1,3,2] 和 [2,4,3],length = 5

  0 0 0 0 0

  先遍历updates, 把开头和结尾标记

  0 2 0 0 -2  index 1 = 2;index 3+1 = -2;

  0 2 3 0 -2  index 2  = 3;index 4+1 超出了范围,就不用处理。

  遍历res array,进行累加 sum += res[i], res[i] = sum

  0 2 3 0 -2  sum = 0+0

  0 2 3 0 -2  sum = 0+2

  0 2 5 0 -2  sum = 2+3

  0 2 5 5 -2  sum = 5+0

  0 2 5 5 3   sum = 5-2  

  可以看到,从第一个范围开头开始,sum 进行累加,并更新number,如果遇到另一个范围,继续累加,如果遇到任何一个范围结束,把那一个范围累加的值减去,这个范围的加法就结束了,继续其他的。

Java Solution:

Runtime beats 77.60%

完成日期:09/16/2017

关键词:Array

关键点:只需要标记范围开始,和结束的位置,之后进行累加

 1 class Solution
 2 {
 3     public int[] getModifiedArray(int length, int[][] updates)
 4     {
 5         int [] res = new int[length];
 6
 7         // iterate each operation
 8         for(int[] update: updates)
 9         {
10             int start = update[0];
11             int end = update[1];
12             int val = update[2];
13
14             // mark first element
15             res[start] += val;
16             // mark last element (end + 1)
17             if(end + 1 < length)
18                 res[end + 1] -= val;
19         }
20
21         int sum = 0;
22         for(int i=0; i<length; i++)
23         {
24             sum += res[i];
25             res[i] = sum;
26         }
27
28         return res;
29     }
30 }

参考资料:

https://discuss.leetcode.com/topic/49691/java-o-k-n-time-complexity-solution

LeetCode 题目列表 - LeetCode Questions List

时间: 2024-10-08 18:13:40

LeetCode 370. Range Addition (范围加法)$的相关文章

LeetCode 598. Range Addition II (区域范围内加法)

Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for

[LeetCode] 598. Range Addition II

https://leetcode.com/problems/range-addition-ii 思路1: brute force - O(m * n * k), k = number of ops思路2: 从 ops 中找到被操作次数最多的 row 和 column,这可以通过遍历 ops 数组,分别找到 row 和 column 的最小值来完成参考: https://discuss.leetcode.com/topic/90547/java-solution-find-min public c

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 a

LeetCode Range Addition II

原题链接在这里:https://leetcode.com/problems/range-addition-ii/description/ 题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two 

LeetCode Range Addition

原题链接在这里:https://leetcode.com/problems/range-addition/description/ 题目: 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 incre

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

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

Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for

[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

[LeetCode&amp;Python] Problem 598. Range Addition II

Given an m * n matrix M initialized with all 0's and several update operations. Operations are represented by a 2D array, and each operation is represented by an array with two positive integers a and b, which means M[i][j] should be added by one for