zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Android开发 设置控件边角半圆
    Android开发 使用XML文件设置背景变化
    Android开发 ImageButton加载drawable文件问题
    Android开发 网络检测(使用广播监听)
    android studio无法在可视化页面预览布局文件
    jdbc连接oracle数据库
    「数学」- 暑假后文化课能力复健 20210906 ~ 20210912
    「数学」- 暑假后文化课能力复健 20210830 ~ 20210905
    「数学」高等代数学习笔记
    「数学」浅谈极值点偏移问题的处理技巧
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7534548.html
Copyright © 2011-2022 走看看