JAVA 100% faster than other codes | Easy to understand
Code


# Code
```java []


class Solution {
    public long maximumCoins(int[][] coins, int k) {
        int n = coins.length;
        
        // Sort intervals by start time
        Arrays.sort(coins, (a, b) -> a[0] - b[0]);

        // Prepare start, end, and sum arrays
        int[] start = new int[n];
        int[] end = new int[n];
        long[] sum = new long[n];

        for (int i = 0; i < n; i++) {
            start[i] = coins[i][0];
            end[i] = coins[i][1];
            long total = (end[i] - start[i] + 1L) * coins[i][2];
            sum[i] = i > 0 ? sum[i - 1] + total : total;
        }

        long ans = 0;

        // *** From Start ***
        for (int i = 0; i < n; i++) {
            int ind = start[i] + k - 1; // End index of k-length interval starting from start[i]
            if (ind <= end[i]) {
                // Entire k-length interval fits within the current interval
                ans = Math.max(ans, k * (long) coins[i][2]);
                continue;
            }

            // Find the farthest interval that overlaps or is included
            int maxInd = findRangeIndex1(ind, end);
            if (maxInd == -1) continue; // No valid range

            long totalCoins = sum[maxInd] - (i > 0 ? sum[i - 1] : 0);

            // Add partial contribution if next interval starts before `ind`
            if (maxInd + 1 < n && ind >= start[maxInd + 1]) {
                totalCoins += (ind - start[maxInd + 1] + 1L) * coins[maxInd + 1][2];
            }

            ans = Math.max(ans, totalCoins);
        }

        // *** From End ***
        for (int i = 0; i < n; i++) {
            int ind = end[i] - k + 1; // Start index of k-length interval ending at end[i]
            if (ind >= start[i]) {
                // Entire k-length interval fits within the current interval
                ans = Math.max(ans, k * (long) coins[i][2]);
                continue;
            }

            // Find the nearest interval that overlaps or is included
            int maxInd = findRangeIndex2(ind, start);
            if (maxInd == -1) continue; // No valid range

            long totalCoins = sum[i] - (maxInd > 0 ? sum[maxInd - 1] : 0);

            // Add partial contribution if previous interval ends after `ind`
            if (maxInd - 1 >= 0 && ind <= end[maxInd - 1]) {
                totalCoins += (end[maxInd - 1] - ind + 1L) * coins[maxInd - 1][2];
            }

            ans = Math.max(ans, totalCoins);
        }

        return ans;
    }

    // Find the farthest index where arr[index] <= ind
    public int findRangeIndex1(int ind, int[] arr) {
        int st = 0, end = arr.length - 1, ans = -1;
        while (st <= end) {
            int mid = st + (end - st) / 2;
            if (arr[mid] <= ind) {
                ans = mid;
                st = mid + 1;
            } else {
                end = mid - 1;
            }
        }
        return ans;
    }

    // Find the nearest index where arr[index] >= ind
    public int findRangeIndex2(int ind, int[] arr) {
        int st = 0, end = arr.length - 1, ans = -1;
        while (st <= end) {
            int mid = st + (end - st) / 2;
            if (arr[mid] >= ind) {
                ans = mid;
                end = mid - 1;
            } else {
                st = mid + 1;
            }
        }
        return ans;
    }
}

```
['Java']