An elegant two-pass sliding window solution with O(n log n) time complexity 
Intuition
The problem involves finding a k-length segment on a number line that collects the maximum possible coins from overlapping segments.
Since we need to
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
- The problem involves finding a k-length segment on a number line that collects the maximum possible coins from overlapping segments. 
- Since we need to consider how segments overlap with our k-length window, we can use a sliding window approach. 
- The key insight is that the optimal k-length segment must either start at the beginning of an existing segment or end at the end of an existing segment.

# Approach
<!-- Describe your approach to solving the problem. -->
1. We consider two cases:

    - Case 1: The k-length segment starts at the start of an existing segment
    - Case 2: The k-length segment ends at the end of an existing segment


2. For Case 1:

    - Sort segments by start position
    - For each segment start position, consider a k-length window starting there
    - Use a sliding window to track segments that overlap with current k-length window
    - Handle partial overlaps at window boundaries


3. For Case 2:

    - Sort segments by end position
    - For each segment end position, consider a k-length window ending there
    - Similar sliding window approach but working backwards
    - Handle partial overlaps at window boundaries


4. Track the maximum coins possible across all possible window positions
# Complexity
- Time complexity: $$O(n \log n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ --> 
    - Sorting operations require O(n log n)
    - Each segment is processed at most twice
    - Window pointers traverse the array once
    
- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

    - Only constant extra space is used for variables (result, currentSum, pointers)
    - The sorting is performed in-place on the input array
    - No additional data structures are required
    - The algorithm maintains a fixed number of variables regardless of input size
    - The temporary variables for window calculations use constant space
# Code
```cpp []
#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    long long maximumCoins(vector<vector<int>>& coins, int k) {
        // Sort the segments based on their start positions
        sort(coins.begin(), coins.end(), [&](const vector<int>& a, const vector<int>& b) -> bool {
            return a[0] < b[0];
        });

        int n = coins.size();
        long long result = 0;

        // -------------------------------
        // Case 1: k-length segment starts at coins[i][0]
        // -------------------------------
        long long currentSum1 = 0;
        int j1 = 0; // Pointer for sliding window in Case 1

        for(auto& segment : coins){
            int windowStart = segment[0];
            int windowEnd = windowStart + k - 1;

            // Add segments that fully lie within [windowStart, windowEnd]
            while(j1 < n && coins[j1][1] < windowEnd){
                currentSum1 += 1LL * (coins[j1][1] - coins[j1][0] + 1) * coins[j1][2];
                j1++;
            }

            // Add partial overlap if any
            long long partial1 = 0;
            if(j1 < n && coins[j1][0] <= windowEnd){
                partial1 = 1LL * (windowEnd - coins[j1][0] + 1) * coins[j1][2];
            }

            // Update the result with the current window's total coins
            result = max(result, currentSum1 + partial1);

            // Remove the contribution of the current segment as the window slides forward
            currentSum1 -= 1LL * (segment[1] - segment[0] + 1) * segment[2];
        }

        // -------------------------------
        // Case 2: k-length segment ends at coins[i][1]
        // -------------------------------
        // Sort the segments based on their end positions for Case 2
        sort(coins.begin(), coins.end(), [&](const vector<int>& a, const vector<int>& b) -> bool {
            return a[1] < b[1];
        });

        long long currentSum2 = 0;
        int j2 = n - 1; // Pointer for sliding window in Case 2

        for(int i = n - 1; i >= 0; i--){
            auto& segment = coins[i];
            int windowEnd = segment[1];
            int windowStart = windowEnd - k + 1;

            // Add segments that fully lie within [windowStart, windowEnd]
            while(j2 >=0 && coins[j2][0] > windowStart){
                currentSum2 += 1LL * (coins[j2][1] - coins[j2][0] + 1) * coins[j2][2];
                j2--;
            }

            // Add partial overlap if any
            long long partial2 = 0;
            if(j2 >=0 && coins[j2][1] >= windowStart){
                partial2 = 1LL * (coins[j2][1] - windowStart + 1) * coins[j2][2];
            }

            // Update the result with the current window's total coins
            result = max(result, currentSum2 + partial2);

            // Remove the contribution of the current segment as the window slides backward
            currentSum2 -= 1LL * (segment[1] - segment[0] + 1) * segment[2];
        }

        return result;
    }
};

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