This content originally appeared on DEV Community and was authored by Piyush Acharya
Intuition
To convert a temperature from Celsius to Kelvin and Fahrenheit, we can use the following formulas:
- Kelvin = Celsius + 273.15
- Fahrenheit = Celsius * 1.80 + 32.00
These formulas are based on the definitions of the temperature scales and the relationships between them.
Approach
We can implement a function that takes a Celsius temperature as input and returns a list of two elements: the corresponding Kelvin and Fahrenheit temperatures. We can use the formulas above to calculate the conversions and round the results to two decimal places.
Complexity
Time complexity: $$O(1)$$
Space complexity: $$O(1)$$
Code
class Solution(object):
def convertTemperature(self, celsius):
return [celsius + 273.15, celsius * 1.80 + 32.00]
"""
:type celsius: float
:rtype: List[float]
"""
This content originally appeared on DEV Community and was authored by Piyush Acharya
Piyush Acharya | Sciencx (2023-05-30T20:08:40+00:00) 2469. LeetCode’s Convert the Temperature – SUPER Simple Python Solution Beats 98% in Memory. Retrieved from https://www.scien.cx/2023/05/30/2469-leetcodes-convert-the-temperature-super-simple-python-solution-beats-98-in-memory/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.