Back to blog
Sep 18, 2024
3 min read

Convert the Temperature

Convert Celsius to Kelvin and Fahrenheit using simple mathematical formulas.

Difficulty: Easy | Acceptance: 90.30% | Paid: No Topics: Math

You are given a non-negative floating point number rounded to two decimal places celsius, which denotes the temperature in Celsius.

You should convert Celsius into Kelvin and Fahrenheit and return it as an array ans = [kelvin, fahrenheit].

Return the array ans.

Note that:

Examples

Example 1:

Input: celsius = 36.50
Output: [309.65000,97.70000]
Explanation: Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.

Example 2:

Input: celsius = 122.11
Output: [395.26000,251.79800]
Explanation: Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.

Constraints

0 <= celsius <= 1000

Direct Formula Approach

Intuition Apply the given conversion formulas directly to compute Kelvin and Fahrenheit from Celsius.

Steps

  • Calculate Kelvin by adding 273.15 to Celsius
  • Calculate Fahrenheit by multiplying Celsius by 1.80 and adding 32.00
  • Return both values as an array
python
class Solution:
    def convertTemperature(self, celsius: float) -> list[float]:
        kelvin = celsius + 273.15
        fahrenheit = celsius * 1.80 + 32.00
        return [kelvin, fahrenheit]

Complexity

  • Time: O(1)
  • Space: O(1)
  • Notes: Constant time and space as we only perform two arithmetic operations.