1 public class Solution {
 2     public int[] getModifiedArray(int length, int[][] updates) {
 3         int[] result = new int[length];
 4         if (updates.length == 0) {
 5             return result;
 6         }
 7         
 8         for (int[] update : updates) {
 9             result[update[1]] += update[2];
10             if (update[0] > 0) {
11                 result[update[0] - 1] -= update[2];
12             }
13         }
14         
15         for (int i = length - 2; i >= 0; i--) {
16             result[i] += result[i+1];
17         }
18         return result;
19     }
20 }

 

1. Do not need to update all the element from i to j. Since it starts with all 0 array. We can simple use start and end as the mark, sum all the values from end to begin.

2. So (i , j) can be treated as (0, i) and (0, j). Sum(j, 0) will update all the element that updated in (0, j) +ins . At same time we need to decrease the same amount for (0, i) as Sum(i, 0) -ins.

posted on 2016-06-29 14:24  keepshuatishuati  阅读(140)  评论(0编辑  收藏  举报