Sliding Window C++
Code


# Code
```cpp []
class Solution {
public:
    long long maximumCoins(vector<vector<int>>& coins, int k) {
        sort(coins.begin(),coins.end());
        int n = coins.size();
        long long ans = 0;
        int j=0;
        long long sum=0;
        for(int i=0;i<n;i++){
            while(j<n && coins[j][1]<=coins[i][0]+k-1){
                sum+=(long long)(coins[j][1]-coins[j][0]+1)*coins[j][2];
                j++;
            }
            long long partial = 0;
            if(j<n && coins[i][0]+k-1>=coins[j][0])
            partial =(long long) (coins[i][0]+k-1-coins[j][0]+1)*coins[j][2];
            ans = max(ans,sum+partial);
            
            sum-=(long long)(coins[i][1]-coins[i][0]+1)*coins[i][2];
        }
        j=n-1;
        sum=0;
        for(int i=n-1;i>=0;i--){
            while(j>=0 && coins[j][0]>=coins[i][1]-k+1){
                sum+=(long long)(coins[j][1]-coins[j][0]+1)*coins[j][2];
                j--;
            }
            long long partial = 0;
            if(j>=0 && coins[i][1]-k+1<=coins[j][1]){
                //cout<<coins[j][1]<<" "<<coins[i][1]-k+1<<" "<<coins[j][2];
                long long diff = coins[j][1] - (coins[i][1]-k);
                partial = (long long)(diff)*coins[j][2];
            }
            ans = max(ans,sum+partial);
            //cout<<sum<<"  "<<partial<<endl;
            sum-=(long long)(coins[i][1]-coins[i][0]+1)*coins[i][2];
        }
        return ans;

    }
};
```
['Sliding Window', 'C++']