Ctrl K

One-Degree Cooling Spells

Medium
A weather station logs the temperature every hour: readings[i] is the reading for hour i. A *cooling spell* is a contiguous stretch of one or more hours in which every reading is exactly one degree below the reading before it. The first hour of a stretch is exempt, so any single hour is a cooling spell too. Return the number of cooling spells in the log.
Example 1
readings=[5, 4, 3, 7, 6] → 9

The run 5, 4, 3 gives 6 stretches: three single hours, [5, 4], [4, 3] and [5, 4, 3]. The run 7, 6 adds 3 more, for 9 in total.

Example 2
readings=[2, 2, 1] → 4

The valid stretches are [2], [2], [1] and [2, 1] from the second and third hours. The first two hours have no drop at all, so they do not form a spell.

Example 3
readings=[9] → 1
Constraints
  • 1 ≤ len(readings) ≤ 10⁵
  • -10⁵ ≤ readings[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNth Number Built From Given Primes MediumChain of Multiples MediumBest Rotation for a Weighted Sum MediumDuel for the End Cards MediumBiggest Division Chain Medium
loading Python…
Input
readings=[5, 4, 3, 7, 6]
Expected
9

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