#2469
Convert the Temperature
EasyMathMathematical calculationsDirect formula application
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(1) | O(1) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(1)Space O(1)
The optimal solution is essentially the same as the brute force approach in this case since the calculations are direct and do not require any iterations or complex data structures. However, we can emphasize that this is the most efficient way to convert temperatures due to the simplicity of the formulas.
⚙️
Algorithm
5 steps- 1Step 1: Read the input temperature in Celsius.
- 2Step 2: Calculate Kelvin using the formula: Kelvin = Celsius + 273.15.
- 3Step 3: Calculate Fahrenheit using the formula: Fahrenheit = Celsius * 1.80 + 32.00.
- 4Step 4: Store both results in an array.
- 5Step 5: Return the array.
solution.py4 lines
1def convertTemperature(celsius):
2 kelvin = celsius + 273.15
3 fahrenheit = celsius * 1.80 + 32.00
4 return [kelvin, fahrenheit]ℹ
Complexity note: The time and space complexity remain O(1) as we are performing a fixed number of calculations and storing a constant size output.
- 1Direct application of formulas leads to efficient solutions.
- 2Understanding the relationship between temperature scales is crucial.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.