Rust Solution
IntuitionThe goal of this problem is to maximize the total number of coins we can collect within a range of length k. We are given a list of intervals where eac
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The goal of this problem is to maximize the total number of coins we can collect within a range of length k. We are given a list of intervals where each interval has a start, end, and the number of coins within that interval. To solve this, we need to figure out how to select and accumulate coins from overlapping or adjacent intervals such that the total number of coins is maximized without exceeding a range of length k.

We can approach this problem by sorting the intervals and using two sliding windows or two pointers: one starting from the beginning of the range and another moving as we expand the interval. By maintaining the number of coins within each range and checking when we can maximize it, we can calculate the maximum coins we can collect.

# Approach
<!-- Describe your approach to solving the problem. -->
*   Sorting the intervals: First, sort the intervals based on the start of the intervals. This allows us to check and compare each interval in a natural, sequential order.

*   Sliding window with two pointers: We use two pointers (i and j) to explore the range of intervals that can fit within the length k. The first pointer (i) iterates over the start of the intervals, while the second pointer (j) moves to include as many intervals as possible that fall within the valid range of length k.

*   Accumulate coins: For each interval, we calculate the total coins that can be collected within a range that starts from the first pointer and extends to the second pointer. We maintain a running sum of coins as we slide the window and update the maximum result.

*   Edge cases: Handle situations where intervals do not overlap or the range extends beyond the valid length k.

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

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

# Code
```rust []
impl Solution {
    pub fn maximum_coins(coins: Vec<Vec<i32>>, k: i32) -> i64 {
        let mut coins = coins;
        coins.sort_by_key(|a| a[0]);
        let n = coins.len() as i64;
        let mut res = 0;
        let mut cur = 0;
        let mut j = 0;
        for i in 0..n {
            while j < n && coins[j as usize][1] <= coins[i as usize][0] + k - 1 {
                cur += (coins[j as usize][1] - coins[j as usize][0] + 1) as i64 * coins[j as usize][2] as i64;
                j += 1;
            }
            if j < n {
                let part = (std::cmp::max(0, coins[i as usize][0] + k - 1 - coins[j as usize][0] + 1)) as i64 * coins[j as usize][2] as i64;
                res = std::cmp::max(res, cur + part);
            }
            cur -= (coins[i as usize][1] - coins[i as usize][0] + 1) as i64 * coins[i as usize][2] as i64;
        }
        cur = 0;
        j = 0;
        for i in 0..n {
            cur += (coins[i as usize][1] - coins[i as usize][0] + 1) as i64 * coins[i as usize][2] as i64;
            while coins[j as usize][1] < coins[i as usize][1] - k + 1 {
                cur -= (coins[j as usize][1] - coins[j as usize][0] + 1) as i64 * coins[j as usize][2] as i64;
                j += 1;
            }
            let part = (std::cmp::max(0, coins[i as usize][1] - k - coins[j as usize][0] + 1)) as i64 * coins[j as usize][2] as i64;
            res = std::cmp::max(res, cur - part);
        }
        res
    }
}
```
['Rust']