Ctrl K

Richest Row of Houses

Medium
Houses numbered 1, 2, 3, … stand along an endless street, and some of them hold coins. The array ranges describes them: a triple ranges[i] = [l, r, c] means every house numbered from l to r inclusive holds c coins. The ranges in ranges do not overlap; houses outside every range hold no coins. Pick length houses standing next to each other and take all their coins. Return the largest number of coins you can collect.
Example 1
ranges=[[2, 4, 3], [7, 8, 5], [12, 12, 9]], length=6 → 19

The best pick is houses 7 through 12: 5 + 5 from houses 7 and 8, nothing from 9–11 and 9 from house 12, 19 in total. Houses 2 through 7 give only 3 · 3 + 5 = 14.

Example 2
ranges=[[5, 9, 2]], length=10 → 10

A window of 10 houses covers all of houses 5–9, each holding 2 coins: 5 · 2 = 10.

Example 3
ranges=[[1, 3, 4], [4, 6, 1]], length=3 → 12

Houses 1–3 give 4 + 4 + 4 = 12; any window further right picks up houses with a single coin and ends up lower.

Constraints
  • 1 ≤ ranges.length ≤ 10⁵
  • ranges[i] = [l, r, c], 1 ≤ l ≤ r ≤ 10⁹, 1 ≤ c ≤ 1000
  • the ranges [l, r] do not overlap
  • 1 ≤ length ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEven Out With Increments MediumRug Over Painted Stretches MediumNudge Values to Agree MediumFlowerbed Beauty on a Budget HardMost Items Within Budget Easy
loading Python…
Input
ranges=[[2, 4, 3], [7, 8, 5], [12, 12, 9]], length=6
Expected
19

Run checks the first 3 tests in your browser. Submit checks all 115 tests on the server, hidden ones included.