forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
55 lines (43 loc) · 1.19 KB
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// Source : https://leetcode.com/problems/range-addition/description/
/// Author : liuyubobobo
/// Time : 2017-10-28
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
/// Range Caching
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
vector<int> res(length, 0);
for(vector<int> update: updates){
int start = update[0];
int end = update[1];
int inc = update[2];
res[start] += inc;
if(end != length - 1)
res[end + 1] -= inc;
}
for(int i = 1 ; i < length ; i ++)
res[i] += res[i-1];
return res;
}
};
int main() {
int length = 5;
int updates[5][3] = {
{1, 3, 2},
{2, 4, 3},
{0, 2, -2}
};
vector<vector<int>> vec;
for(int i = 0 ; i < length ; i ++)
vec.push_back(vector<int>(updates[i], updates[i] + sizeof(updates[i])/sizeof(int)));
vector<int> res = Solution().getModifiedArray(length, vec);
for(int e: res)
cout << e << " ";
cout << endl;
return 0;
}