Ctrl K

Narrow the Temperature Spread

Easy
A building has several rooms, and temps[i] is the temperature in room i. Each room's thermostat may be adjusted once (or left alone), shifting that room's temperature by any whole number of degrees from -limit to limit. The spread is the difference between the warmest and the coldest room. Return the smallest spread you can achieve.
Example 1
temps=[18, 25, 21], limit=2 → 3

Raise the coldest room to 20, lower the warmest to 23 and leave the third at 21: the spread is 23 - 20 = 3. You cannot do better.

Example 2
temps=[12, 15, 14, 13], limit=2 → 0

Every room can be set to 13 or 14 degrees, so the spread is 0.

Example 3
temps=[7], limit=0 → 0

With a single room the warmest and the coldest are the same.

Constraints
  • 1 ≤ len(temps) ≤ 10⁴
  • 0 ≤ temps[i] ≤ 10⁴
  • 0 ≤ limit ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdd One to a Digit Array EasyThe Lost Ticket EasyCorner Stamps on a Table EasyBest Product of Three EasyThe Biggest Triangle on the Board Easy
loading Python…
Input
temps=[18, 25, 21], limit=2
Expected
3

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