Align with start or end
IntuitionApproach1 Algin start with each interval
2 ALign end with each interval : here we reverse each interval to make it same as Align with start so we can r
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
1 Algin start with each interval
2 ALign end with each interval : here we reverse each interval to make it same as Align with start so we can reuse the code.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(nlogn) for sort
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
typedef long long ll;
struct node
{
    ll x;
    ll y;
    ll prefix;
    ll coins;

    node(int x1, int y1, ll p1, int cs)
    {
        x = x1;
        y = y1;
        prefix = p1;
        coins = cs;
    };
};
class Solution {
public:
    
    long long mxCoins(vector<vector<int>>& cs, int k) {
        sort(cs.begin(), cs.end());
        // start, prefixsum, end, coins, last start is INT_MAX
        // end, prefixsum to end, start, coins
        vector<node> prefix;
        ll pp = 0;
        for (auto c : cs)
        {
            prefix.push_back(node(c[0], c[1], pp, c[2]));
            //cout << 
            pp += (ll)(c[1] - c[0] + 1) * (ll)c[2];
        }
        prefix.push_back(node(INT_MAX, INT_MAX, pp, 0));
         auto cmp = [](const node& a, const node& b) { return a.y < b.y; };
        sort(prefix.begin(), prefix.end(), cmp);

       
        
        int n = prefix.size();
        ll ans = 0;
        int j = 0;
        for (int i = 0; i < n - 1; i++)
        {
            int l = prefix[i].x;
            int r = l + k - 1;
            ll pre = prefix[i].prefix;
            ll v = 0;
            node cur = {r, r, 0, 0};
            while (j < n && prefix[j].y < r)
            {
                j++;
            }
            node* iter = &(prefix[j]);
            if (iter->y == INT_MAX)
            {
                v = iter->prefix - pre;
            }
            else 
            {
                v = iter->prefix - pre;
                if (r >= iter->x)
                {
                    v += (r - iter->x + 1) * iter->coins;
                }
            }
            //cout << l << "," << r << "," << v << "," << pre << "," << iter->prefix << endl;
            ans = max(ans, v);
        }
        //cout << ans << endl;
        return ans;
    }

    long long maximumCoins(vector<vector<int>>& cs, int k) {
        ll a = mxCoins(cs, k);
        int x = INT_MAX;
        int y = INT_MIN;
        for (auto& c : cs)
        {
            x = min(x, c[0]);
            y = max(y, c[1]);
        }
        vector<vector<int>> cs1;
        for (auto& c : cs)
        {
            int a = c[0];
            int b = c[1];

            int l = y - b;
            int r = y - a;
            cs1.push_back({l, r, c[2]});
        }

        ll b = mxCoins(cs1, k);
        return max(a, b);
    };
};
```
['C++']