|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/**Assume you have an array of length n initialized with all 0's and are given k update operations. |
4 |
| -
|
5 |
| - 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. |
6 |
| -
|
7 |
| - Return the modified array after all k operations were executed. |
| 3 | +/** |
| 4 | + * 370. Range Addition |
| 5 | + * |
| 6 | + * Assume you have an array of length n initialized with all 0's and are given k update operations. |
| 7 | + * Each operation is represented as a triplet: [startIndex, endIndex, inc] |
| 8 | + * which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc. |
| 9 | + * Return the modified array after all k operations were executed. |
8 | 10 |
|
9 | 11 | Example:
|
10 | 12 |
|
|
33 | 35 |
|
34 | 36 | After applying operation [0, 2, -2]:
|
35 | 37 | [-2, 0, 3, 5, 3 ]
|
36 |
| - Hint: |
37 | 38 |
|
| 39 | + Hint: |
38 | 40 | Thinking of using advanced data structures? You are thinking it too complicated.
|
39 | 41 | For each update operation, do you really need to update all elements between i and j?
|
40 | 42 | Update only the first and end element is sufficient.
|
41 |
| - The optimal time complexity is O(k + n) and uses O(1) extra space.*/ |
| 43 | + The optimal time complexity is O(k + n) and uses O(1) extra space. |
| 44 | + */ |
42 | 45 |
|
43 | 46 | public class _370 {
|
44 |
| - /**Previously AC'ed brute force solution results in TLE now.*/ |
45 |
| - public static int[] getModifiedArray_TLE(int length, int[][] updates) { |
46 |
| - int[] nums = new int[length]; |
47 |
| - int k = updates.length; |
48 |
| - for (int i = 0; i < k; i++) { |
49 |
| - int start = updates[i][0]; |
50 |
| - int end = updates[i][1]; |
51 |
| - int inc = updates[i][2]; |
52 |
| - for (int j = start; j <= end; j++) { |
53 |
| - nums[j] += inc; |
| 47 | + public static class Solution1 { |
| 48 | + public int[] getModifiedArray(int length, int[][] updates) { |
| 49 | + int[] nums = new int[length]; |
| 50 | + int k = updates.length; |
| 51 | + for (int i = 0; i < k; i++) { |
| 52 | + int start = updates[i][0]; |
| 53 | + int end = updates[i][1]; |
| 54 | + int inc = updates[i][2]; |
| 55 | + nums[start] += inc; |
| 56 | + if (end < length - 1) { |
| 57 | + nums[end + 1] -= inc; |
| 58 | + } |
54 | 59 | }
|
55 |
| - } |
56 |
| - return nums; |
57 |
| - } |
58 | 60 |
|
59 |
| - /** |
60 |
| - * Looked at this post: https://discuss.leetcode.com/topic/49691/java-o-k-n-time-complexity-solution and one OJ official article: https://leetcode.com/articles/range-addition/ |
61 |
| - */ |
62 |
| - public static int[] getModifiedArray(int length, int[][] updates) { |
63 |
| - int[] nums = new int[length]; |
64 |
| - int k = updates.length; |
65 |
| - for (int i = 0; i < k; i++) { |
66 |
| - int start = updates[i][0]; |
67 |
| - int end = updates[i][1]; |
68 |
| - int inc = updates[i][2]; |
69 |
| - nums[start] += inc; |
70 |
| - if (end < length - 1) { |
71 |
| - nums[end + 1] -= inc; |
| 61 | + int sum = 0; |
| 62 | + for (int i = 0; i < length; i++) { |
| 63 | + sum += nums[i]; |
| 64 | + nums[i] = sum; |
72 | 65 | }
|
73 |
| - } |
74 |
| - |
75 |
| - int sum = 0; |
76 |
| - for (int i = 0; i < length; i++) { |
77 |
| - sum += nums[i]; |
78 |
| - nums[i] = sum; |
79 |
| - } |
80 |
| - return nums; |
81 |
| - } |
82 |
| - |
83 |
| - public static void main(String... args) { |
84 |
| - /**5 |
85 |
| - [[1,3,2],[2,4,3],[0,2,-2]]*/ |
86 |
| - int length = 5; |
87 |
| - int[][] updates = new int[][]{ |
88 |
| - {1, 3, 2}, |
89 |
| - {2, 4, 3}, |
90 |
| - {0, 2, -2}, |
91 |
| - }; |
92 |
| - int[] result = getModifiedArray(length, updates); |
93 |
| - for (int i : result) { |
94 |
| - System.out.print(i + "\t"); |
| 66 | + return nums; |
95 | 67 | }
|
96 | 68 | }
|
97 | 69 | }
|
0 commit comments