File tree Expand file tree Collapse file tree 1 file changed +0
-43
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +0
-43
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
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.
10
-
11
- Example:
12
-
13
- Given:
14
-
15
- length = 5,
16
- updates = [
17
- [1, 3, 2],
18
- [2, 4, 3],
19
- [0, 2, -2]
20
- ]
21
-
22
- Output:
23
-
24
- [-2, 0, 3, 5, 3]
25
- Explanation:
26
-
27
- Initial state:
28
- [ 0, 0, 0, 0, 0 ]
29
-
30
- After applying operation [1, 3, 2]:
31
- [ 0, 2, 2, 2, 0 ]
32
-
33
- After applying operation [2, 4, 3]:
34
- [ 0, 2, 5, 5, 3 ]
35
-
36
- After applying operation [0, 2, -2]:
37
- [-2, 0, 3, 5, 3 ]
38
-
39
- Hint:
40
- Thinking of using advanced data structures? You are thinking it too complicated.
41
- For each update operation, do you really need to update all elements between i and j?
42
- Update only the first and end element is sufficient.
43
- The optimal time complexity is O(k + n) and uses O(1) extra space.
44
- */
45
-
46
3
public class _370 {
47
4
public static class Solution1 {
48
5
public int [] getModifiedArray (int length , int [][] updates ) {
You can’t perform that action at this time.
0 commit comments