Python Two Pointers
IntuitionEither start or end of the segment aligns with the start or end of any one interval respectively.Start from the interval which aligns and keep on addin
# Intuition
Either start or end of the segment aligns with the start or end of any one interval respectively.

Start from the interval which aligns and keep on adding intervals if they are fully in the range and at last, add the partial interval. Do the same in the reversed order too.


# Complexity
- Time complexity:
O(nlogn)

- Space complexity:
Space taken by the sorting algorithm.

# Code
```python3 []
class Solution:
    def maximumCoins(self, coins: List[List[int]], k: int) -> int:
        coins.sort(key = lambda coin : coin[0])
        
        ans = 0
        curr = 0

        #Start from first, start of segment aligns with start of interval
        j = 0
        for i, (l, r, c) in enumerate(coins):
            #Entire interval falls in range
            while j < len(coins) and coins[j][1] < l + k:
                curr += (coins[j][1] - coins[j][0] + 1) * coins[j][2]
                j += 1
            
            #Partial interval falls in range
            extra = 0
            if j < len(coins) and coins[j][0] < l + k:
                extra = (l + k - coins[j][0]) * coins[j][2]
            
            ans = max(ans, curr + extra)
            curr -= (r - l + 1) * c        

        #Start from end, end of segment aligns with end of interval
        curr = 0
        j = len(coins) - 1
        for i in range(len(coins) - 1, -1, -1):
            l, r, c = coins[i]
            #Entire interval falls in range
            while j >= 0 and coins[j][0] > r - k:
                curr += (coins[j][1] - coins[j][0] + 1) * coins[j][2]
                j -= 1
            
            #Partial interval falls in range
            extra = 0
            if j >= 0 and coins[j][1] > r - k:
                extra = (coins[j][1] - (r - k)) * coins[j][2]
            
            ans = max(ans, curr + extra)
            curr -= (r - l + 1) * c

        return ans

```
['Two Pointers', 'Sorting', 'Python3']