Convert the Temperature
EasyPrompt
You are given a non-negative floating point number rounded to two decimal places celsius, that 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. Answers within 10-5 of the actual answer will be accepted.
Note that:
Kelvin = Celsius + 273.15Fahrenheit = Celsius * 1.80 + 32.00
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
Approaches
1 approach with complexity analysis and trade-offs.
This problem is a straightforward application of mathematical formulas. The most efficient and logical approach is to directly implement the given conversion formulas to calculate the Kelvin and Fahrenheit temperatures from the input Celsius value.
Algorithm
- Define a function that takes a single floating-point argument,
celsius. - Calculate the temperature in Kelvin using the formula:
Kelvin = celsius + 273.15. - Calculate the temperature in Fahrenheit using the formula:
Fahrenheit = celsius * 1.80 + 32.00. - Create a new array of doubles with a size of 2.
- Store the calculated Kelvin value at the first index (index 0) of the array.
- Store the calculated Fahrenheit value at the second index (index 1) of the array.
- Return the newly created array.
Walkthrough
The solution involves performing two simple arithmetic operations based on the formulas provided in the problem description.
First, we calculate the Kelvin temperature by adding 273.15 to the input celsius.
Second, we calculate the Fahrenheit temperature by multiplying the celsius value by 1.80 and then adding 32.00.
Finally, these two resulting values are placed into a new array of doubles of size two, which is then returned. This approach is optimal as it involves a constant number of operations.
class Solution { public double[] convertTemperature(double celsius) { // Calculate Kelvin using the provided formula double kelvin = celsius + 273.15; // Calculate Fahrenheit using the provided formula double fahrenheit = celsius * 1.80 + 32.00; // Return the results in a new double array return new double[]{kelvin, fahrenheit}; }}Complexity
Time
O(1) - The time complexity is constant because the solution involves a fixed number of arithmetic operations (one addition, one multiplication, and one addition). These operations take the same amount of time regardless of the value of `celsius`.
Space
O(1) - The space required is constant. We only need to allocate space for the output array of a fixed size (2), which does not depend on the input value.
Trade-offs
Pros
Extremely simple and easy to understand and implement.
Most efficient solution possible with constant time and space complexity.
Directly addresses the problem requirements without any overhead.
Cons
There are no significant disadvantages to this approach as it is the most direct and optimal solution for this problem.
Solutions
Solution
class Solution {public double[] convertTemperature(double celsius) { return new double[]{celsius + 273.15, celsius * 1.8 + 32}; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.