HackerRank Array 5.Array Manipulation

題目連結:Array Manipulation | HackerRank

只要算開始與結束即可

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;
}