Skip to content

Commit caaa4a6

Browse files
authored
Create range-addition.cpp
1 parent 8bf2373 commit caaa4a6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

C++/range-addition.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(k + n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
7+
vector<int> result(length, 0);
8+
9+
for (const auto& update: updates) {
10+
result[update[0]] += update[2];
11+
if (update[1] + 1 < length) {
12+
result[update[1] + 1] -= update[2];
13+
}
14+
}
15+
16+
for (int i = 1; i < length; ++i) {
17+
result[i] += result[i - 1];
18+
}
19+
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)