O(nlogn) Sliding window solution
IntuitionApproachComplexity
Time complexity: O(nlogn) for sort()

Space complexity: O(1)
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

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

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def maximumCoins(self, coins, k):
        total = 0
        res = 0
        i = 0
        coins.sort()    #O(nlogn)
        for j in range(len(coins)): #j和i各自最多移动n次，所以是O(n)
            l, r, value = coins[j]
            total += (r-l+1)*value    #把右指针的bags加入total
            while coins[j][1]-coins[i][0]+1 > k: #当窗口超过k，需要缩小
                excess = coins[j][1]-coins[i][0]+1-k    #超过的长度  
                res = max(res, total-excess*value)  #缩减右边bag，更新结果，没改变total的值
                il,ir,ivalue=coins[i]
                if ir-il+1 > excess:            #如果左边的区间比excess还长，说明可以部分缩减
                    total -= excess * ivalue    #缩减左边的长度，total是原始值
                    coins[i][0] += excess       #调整左指针的左边界
                else:               #左边不够长的话，需要把整个bag都去掉
                    total -= ivalue * (ir-il+1)
                    i+=1
            res = max(res, total)
        return res

    
```
['Python3']