maxWeightIntervals function

int maxWeightIntervals(
  1. List<WeightedIntervalUtils> intervals
)

Returns the maximum total weight of non-overlapping intervals (DP by end time). Audited: 2026-06-12 11:26 EDT

Implementation

int maxWeightIntervals(List<WeightedIntervalUtils> intervals) {
  if (intervals.isEmpty) return 0;
  // Sort by end time so that for any interval, every compatible (earlier-ending,
  // non-overlapping) interval sits to its left — the precondition that makes the
  // linear back-scan below correct.
  final List<WeightedIntervalUtils> sorted = List<WeightedIntervalUtils>.of(intervals)
    ..sort((a, b) => a.end.compareTo(b.end));
  // Each dp entry holds the best achievable weight using only the first i
  // intervals; the entry at index zero is the empty-selection base case, so dp
  // is sized one larger than the input and indexed from one.
  final List<int> dp = List.filled(sorted.length + 1, 0);
  for (int i = 1; i <= sorted.length; i++) {
    final WeightedIntervalUtils cur = sorted[i - 1];
    // Walk left to the nearest interval that ends at or before the current
    // interval's start; the index reached counts the intervals compatible with
    // taking the current one. Linear scan (not binary search) keeps the code
    // simple; cost is quadratic worst case, fine for the small interval sets
    // this utility targets.
    int j = i - 1;
    while (j > 0 && sorted[j - 1].end > cur.start) {
      j--;
    }
    // Choose the better of skipping the current interval or taking it (its
    // weight plus the best weight among the compatible intervals to its left).
    final int w = (j > 0 ? dp[j] : 0) + cur.weight;
    dp[i] = dp[i - 1] > w ? dp[i - 1] : w;
  }
  return dp[sorted.length];
}