Convert the Temperature (LeetCode 2469): Solution and Explanation

Easy
Math

Given a Celsius temperature celsius between 0 and 1000 with at most two decimals, return [kelvin, fahrenheit], using Kelvin = Celsius + 273.15 and Fahrenheit = Celsius * 1.80 + 32.00.

Intuition

Picture three thermometers standing side by side, all dipped in the same glass of water. The mercury sits at the same height in each one, but the numbers printed beside it differ. Converting is reading the same height off a different scale.

The Kelvin scale uses degrees of the same size as Celsius, it just starts lower: 0 Celsius is printed as 273.15 on the Kelvin tube. So Kelvin is a pure shift, celsius + 273.15.

The Fahrenheit scale changes two things. Its degrees are smaller, with 180 of them between freezing and boiling water where Celsius has 100, so every Celsius degree is 1.8 Fahrenheit degrees. And freezing water reads 32 instead of 0. So first stretch by 1.8, then shift by 32.

The problem accepts answers within 1e-5, so ordinary double arithmetic is enough. There is nothing to loop over.

Approach: Apply both formulas

  1. Compute kelvin = celsius + 273.15.
  2. Compute fahrenheit = celsius * 1.80 + 32.00.
  3. Return the array [kelvin, fahrenheit] in that order.

Step-by-step example

celsius = 36.50
  1. The Celsius thermometer reads 36.5.
  2. Kelvin: shift by 273.15, so 36.5 + 273.15 = 309.65.
  3. Fahrenheit, stretch: 36.5 * 1.8 = 65.7.
  4. Fahrenheit, shift: 65.7 + 32 = 97.7.
  5. Return [309.65, 97.7].

Code: Python, Java, C++

1class Solution:
2    def convertTemperature(self, celsius: float) -> List[float]:
3        kelvin = celsius + 273.15
4        fahrenheit = celsius * 1.80 + 32.00
5        return [kelvin, fahrenheit]
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(1)

Two additions and one multiplication, whatever the input is.

Space

O(1)

Only the two-element answer array is created.

Other approaches

Fahrenheit from Kelvin (time O(1), space O(1))

Compute Kelvin first and then Fahrenheit as (kelvin - 273.15) * 1.8 + 32. It gives the same reading but adds an extra subtraction, which also adds a little floating point noise.

Fraction form 9/5 (time O(1), space O(1))

Write the stretch as celsius * 9 / 5 + 32. It is the same conversion; just make sure the division is done in floating point, not integer division.

Common mistakes

  • Returning the values in the wrong order. Kelvin comes first, Fahrenheit second.
  • Forgetting the +32 shift and only multiplying by 1.8, which reads freezing water as 0 Fahrenheit.
  • Using integer division in celsius * 9 / 5 in a language where the operands could be integers.
  • Rounding the answer to two decimals yourself. The judge compares the raw doubles, and rounding can only make things worse.

Frequently asked questions

Why is Kelvin only a shift but Fahrenheit a stretch and a shift?

A Kelvin degree is exactly as big as a Celsius degree, so only the zero point moves. A Fahrenheit degree is 5/9 of a Celsius degree, so the scale is both stretched and moved.

Do I need to worry about floating point error?

Not here. The problem accepts answers within 1e-5, and a couple of double operations on numbers below 2000 are far more precise than that.

Is there any loop or data structure involved?

No. It is a warm-up problem about reading a formula carefully and returning the right array shape.