Skip to content

Commit 3cf393b

Browse files
committed
370
1 parent 898cc83 commit 3cf393b

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Leetcode solutions in C++, Java, Python
33

44
|#|Title|Solution|Difficulty|
55
|---|-----|--------|----------|
6+
|370|[Range Addition](./src/range_addition)|[C++](./src/range_addition)|Medium|
67
|369|[Plus One Linked List](./src/plus_one_linked_list)|[C++](./src/plus_one_linked_list)|Medium|
78
|368|[Largest divisible subset](./src/largest_divisible_subset)|[C++](./src/largest_divisible_subset)|Medium|
89
|367|[Valid Perfect Square](./src/valid_perfect_square)|[C++](./src/valid_perfect_square)|Medium|

src/range_addition/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all:
2+
clang++ main.cpp -o main.o -std=c++11
3+
4+
run:
5+
clang++ main.cpp -o main.o -std=c++11
6+
./main.o

src/range_addition/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Problem
2+
===
3+
4+
Assume you have an array of length n initialized with all 0's and are given k update operations.
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+
Return the modified array after all k operations were executed.
7+
8+
Example:
9+
10+
Given:
11+
length = 5,
12+
updates = [
13+
[1, 3, 2],
14+
[2, 4, 3],
15+
[0, 2, -2]
16+
]
17+
18+
Output: [-2, 0, 3, 5, 3]
19+
20+
Initial state:
21+
[ 0, 0, 0, 0, 0 ]
22+
23+
After applying operation [1, 3, 2]:
24+
[ 0, 2, 2, 2, 0 ]
25+
26+
After applying operation [2, 4, 3]:
27+
[ 0, 2, 5, 5, 3 ]
28+
29+
After applying operation [0, 2, -2]:
30+
[-2, 0, 3, 5, 3 ]
31+
32+
Solution
33+
===
34+
- Similar to segment tree
35+
- View interval as two operations: one to increase [start, n), another to decrease [end+1, n)
36+
- Pre-process all segments before carrying out increments
37+
- Half-open, half-close intervals are easier to manage

src/range_addition/main.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <algorithm>
2+
#include <cstdlib>
3+
#include <iostream>
4+
#include <map>
5+
#include <queue>
6+
#include <set>
7+
#include <sstream>
8+
#include <stack>
9+
#include <string>
10+
#include <unordered_map>
11+
#include <unordered_set>
12+
#include <vector>
13+
14+
using namespace std;
15+
16+
class Solution {
17+
public:
18+
vector<int> getModifiedArray(int length, vector<vector<int>> updates) {
19+
vector<int> results(length);
20+
for(auto update: updates) {
21+
results[update[0]] += update[2];
22+
if (update[1] + 1 < length) {
23+
results[update[1] + 1] -= update[2];
24+
}
25+
}
26+
int value = 0;
27+
for(int i = 0; i < length; i++) {
28+
value += results[i];
29+
results[i] = value;
30+
}
31+
return results;
32+
}
33+
};
34+
35+
int main() {
36+
Solution sol;
37+
vector<vector<int>> updates = {
38+
{1, 3, 2},
39+
{2, 4, 3},
40+
{0, 2, -2}
41+
};
42+
for (auto i : sol.getModifiedArray(5, updates)) {
43+
cout << i << ' ';
44+
}
45+
return 0;
46+
}

0 commit comments

Comments
 (0)