Problem

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 arrayans. _Answers within 10-5 of the actual answer will be accepted.

Note that:

  • Kelvin = Celsius + 273.15
  • Fahrenheit = Celsius * 1.80 + 32.00

Examples

Example 1

1
2
3
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

1
2
3
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

Solution

Method 1 – Direct Formula Application 1

Intuition

The conversion from Celsius to Kelvin and Fahrenheit is direct and based on well-known formulas. We simply apply these formulas to the input value.

Approach

  1. Given the Celsius value, compute Kelvin as celsius + 273.15.
  2. Compute Fahrenheit as celsius * 1.80 + 32.00.
  3. Return the result as an array [kelvin, fahrenheit].

Code

1
2
3
4
5
6
class Solution {
public:
    vector<double> convertTemperature(double celsius) {
        return {celsius + 273.15, celsius * 1.80 + 32.00};
    }
};
1
2
3
func convertTemperature(celsius float64) []float64 {
    return []float64{celsius + 273.15, celsius*1.80 + 32.00}
}
1
2
3
4
5
class Solution {
    public double[] convertTemperature(double celsius) {
        return new double[]{celsius + 273.15, celsius * 1.80 + 32.00};
    }
}
1
2
3
4
5
class Solution {
    fun convertTemperature(celsius: Double): DoubleArray {
        return doubleArrayOf(celsius + 273.15, celsius * 1.80 + 32.00)
    }
}
1
2
3
class Solution:
    def convertTemperature(self, celsius: float) -> list[float]:
        return [celsius + 273.15, celsius * 1.80 + 32.00]
1
2
3
4
5
impl Solution {
    pub fn convert_temperature(celsius: f64) -> Vec<f64> {
        vec![celsius + 273.15, celsius * 1.80 + 32.00]
    }
}
1
2
3
4
5
class Solution {
    convertTemperature(celsius: number): number[] {
        return [celsius + 273.15, celsius * 1.80 + 32.00];
    }
}

Complexity

  • ⏰ Time complexity: O(1), as only a constant number of operations are performed.
  • 🧺 Space complexity: O(1), as only a constant amount of space is used for the result.