Concept
參考自神奇的Lee215
類似Leetcode Meeting Room
記錄每個start, end作為斷點
每個斷點記錄他的變化值(delta)
start就加、end就減
最後遍歷每個斷點,即可得到答案。
圖片來自:FishballLin
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class Solution {
public:
vector<vector<long long>> splitPainting(vector<vector<int>>& segments) {
map<int, long long> m;
for(int i = 0; i < segments.size(); i++){
m[segments[i][0]] += segments[i][2];
m[segments[i][1]] -= segments[i][2];
}
vector<vector<long long>> res;
int curr = 0;
for(auto it: m){
if(m[curr] > 0){
res.push_back({curr, it.first, m[curr]});
}
m[it.first] += m[curr];
curr = it.first;
}
return res;
}
};
|