Sliding Window
ApproachTechniques Used
The code uses several key techniques to solve the problem efficiently while ensuring all possible valid windows are considered:
Sorting
# Approach

Techniques Used
The code uses several key techniques to solve the problem efficiently while ensuring all possible valid windows are considered:

1. Sorting the Intervals
The intervals are sorted by their start points (l) to process them in ascending order. This helps in iterating over the intervals and calculating the maximum number of coins in a sliding window.

Example
Input Intervals:

```
coins = [[8, 10, 1], [1, 3, 2], [5, 6, 4]];
```
Sorted Intervals:
```
[[1, 3, 2], [5, 6, 4], [8, 10, 1]];
```
By sorting, we ensure that intervals are processed sequentially, which is crucial for the sliding window technique.

---

2. Sliding Window
A sliding window is used to keep track of the total number of coins within a range of size K. For each interval, we:

Add coins from intervals that are fully within the current window.
Consider coins partially from intervals that partially overlap with the window.
Example
Window of Size K = 4:
We want to calculate the number of coins for a window starting at interval [1, 3] (range [1, 4]).

Interval [1, 3, 2]: Fully inside the window.
Coins = (3 − 1 + 1) × 2 = 6.
Interval [5, 6, 4]: Outside the window, ignored.
Result for this window: 6.

---

3. Handling Partial Overlaps
When an interval partially overlaps the window, we calculate how much of it contributes to the total coins by adjusting its range to fit the window boundaries.

Example
Window of Size K = 4:
Window starting at [8, 10] (range [8, 11]).

Interval [8, 10, 1]: Partially inside the window.
Overlap length = min(10, 11) − 8 + 1 = 3.
Coins = 3 × 1 = 3.
Result for this window: 3.

---

4. Reversing the Intervals
After calculating the maximum coins for windows starting at the beginning of intervals, we reverse the intervals by flipping their start and end points. This allows us to compute windows ending at the end of intervals using the same logic.

Example
Original Intervals:

```
[[1, 3, 2], [5, 6, 4], [8, 10, 1]];
```

Reversed Intervals:
```
[[-3, -1, 2], [-6, -5, 4], [-10, -8, 1]];
```
By reversing, we can now consider windows that terminate at the right endpoint of the intervals.


---

The function calculates the maximum coins for:

Windows that start at the beginning of intervals.
Windows that end at the end of intervals.
The final result is the maximum of these two calculations.

---

# Example
Input
```
coins = [[8, 10, 1], [1, 3, 2], [5, 6, 4]];
k = 4;

```
Step 1: Sort the Intervals

```
coins = [[1, 3, 2], [5, 6, 4], [8, 10, 1]];

```

Step 2: Compute Windows Starting at the Beginning
Window [1, 4]:
Coins = 6 + 0 = 6.

Window [5, 8]:
Coins = 4 + 4 = 8.

Window [8, 11]:
Coins = 3 + 0 = 3.

Maximum = 10.

Step 3: Reverse the Intervals

```
coins = [[-3, -1, 2], [-6, -5, 4], [-10, -8, 1]];

```
Step 4: Compute Windows Ending at the End
Window [-3, -6] (equivalent to [1, 3]):
Coins = 6 + 0 = 6.

Window [-5, -8] (equivalent to [5, 8]):
Coins = 4 + 4 = 8.

Window [-8, -11] (equivalent to [8, 11]):
Coins = 3 + 0 = 3.

Maximum = 10.

Step 5: Compare and Return Result
The maximum coins collected is 10.


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

- Space complexity:
- O(1) additional space if in-place modifications are allowed, otherwise O(n) for reversed intervals.
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
function maximumCoins(coins, k) {
  const n = coins.length;

  function solve(intervals) {
    intervals.sort((a, b) => a[0] - b[0]); // Ordenar por inicio del intervalo
    let ans = 0, window = 0, j = 0;

    for (let i = 0; i < intervals.length; i++) {
      const [l, r, c] = intervals[i];

      while (j + 1 < n && intervals[j + 1][0] < l + k) {
        const [lj, rj, cj] = intervals[j];
        window += (rj - lj + 1) * cj;
        j++;
      }

      let extra = 0;
      if (j < n && intervals[j][0] < l + k) {
        const [lj, rj, cj] = intervals[j];
        const rightmost = Math.min(l + k - 1, rj);
        const length = rightmost - lj + 1;
        extra = length * cj;
      }

      ans = Math.max(ans, window + extra);
      window -= (r - l + 1) * c;
    }

    return ans;
  }

  let ans = solve(coins);

  for (let i = 0; i < coins.length; i++) {
    const [l, r, c] = coins[i];
    coins[i] = [-r, -l, c];
  }

  ans = Math.max(ans, solve(coins));
  return ans;
}

```
['JavaScript']