Editor

Rainfall Between Two Days

#624 · Lists · Intermediate · Fast enough · 65 XP · about 12 min

A weather station has recorded the rainfall of every day, in millimeters: rain[0] fell on day 0, rain[1] on day 1, and so on. Farmers keep asking how much rain fell over a stretch of days. Each question is a pair (first, last) of day numbers, and both of those days count.

Write rain_totals(rain, periods) that returns a list with the total rainfall of each period in periods, in the same order.

Examples

rain_totals([3, 0, 5, 2, 8], [(0, 2), (1, 3), (4, 4)])  # returns [8, 7, 8]
rain_totals([1, 1, 1, 1], [(0, 3), (2, 3)])             # returns [4, 2]
rain_totals([6, 2], [])                                 # returns []

The first period runs from day 0 to day 2: 3 + 0 + 5 = 8 millimeters.

Constraints

  • 1 <= len(rain) <= 200,000
  • 0 <= len(periods) <= 200,000
  • 0 <= rain[i] <= 500
  • 0 <= first <= last < len(rain)

Time limit

Each test must finish within 10 seconds, and the hidden tests ask 200,000 questions about 200,000 days.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.