0%

HackerRank Array 5.Array Manipulation

題目連結:Array Manipulation | HackerRank

只要算開始與結束即可

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
struct edge{
int s,e,c;
};

long arrayManipulation(int n, vector<edge> q) {
long mx=0,now=0;
sort(q.begin(),q.end(),[](edge a,edge b){
return a.s<b.s;
});
vector<edge> bk(q);
sort(bk.begin(),bk.end(),[](edge a,edge b){
return a.e<b.e;
});

int i=0;
for(auto a:q){
now+=a.c;
while(i<q.size() && a.s>bk[i].e){
now-=bk[i].c;
i++;
}
mx=max(mx,now);
}
return mx;
}