Leetcode 50: Pow(x, n)
You are given a number
x
and an integer n
. Your task is to compute x
raised to the power n
(i.e., x^n
). Handle cases where the exponent is negative by returning the reciprocal of the result.Problem
Approach
Steps
Complexity
Input: The input consists of a number `x` (float) and an integer `n` (int).
Example: ["x = 3.00000, n = 4"]
Constraints:
• -100.0 < x < 100.0
• -231 <= n <= 231-1
• n is an integer.
• Either x is not zero or n > 0.
Output: The output should be a float value, which is `x` raised to the power `n`.
Example: ["81.00000"]
Constraints:
• The result must be computed within the constraints of the problem.
Goal: The goal is to calculate `x^n` efficiently, especially for large `n` values.
Steps:
• 1. Check if the exponent `n` is zero. If so, return 1.
• 2. If `n` is negative, compute the power of the reciprocal of `x` raised to the positive exponent.
• 3. Use fast exponentiation (divide-and-conquer) for optimal computation, where the power is halved for each recursive step.
Goal: The input consists of a floating point number `x` and an integer `n`.
Steps:
• x is a float in the range (-100.0, 100.0).
• n is an integer between -231 and 231-1.
• x will not be zero when n <= 0.
Assumptions:
• The function should handle both positive and negative exponents correctly.
• The input constraints guarantee that no invalid values (such as x = 0 when n <= 0) will be provided.
• Input: ["x = 3.00000, n = 4"]
• Explanation: In this example, `3^4` is calculated, resulting in `81.00000`.
• Input: ["x = 2.50000, n = -2"]
• Explanation: Here, `2.5^-2` is computed as the reciprocal of `2.5^2`, resulting in `0.16000`.
Approach: The approach uses a divide-and-conquer strategy to calculate the power efficiently. If the exponent is negative, we compute the reciprocal of the power of the positive exponent.
Observations:
• Exponentiation can be computed efficiently using recursion or iteration with halving the exponent.
• Handling negative exponents requires computing the reciprocal of the result.
Steps:
• 1. If the exponent `n` is 0, return 1. This is a special case for any number raised to the power of zero.
• 2. If `n` is negative, compute the power of the reciprocal of `x` raised to the positive exponent, and return that value.
• 3. For a positive `n`, use fast exponentiation by recursively dividing `n` by 2, squaring the base and halving the exponent.
Empty Inputs:
• No empty input values as per the constraints.
Large Inputs:
• For large inputs, the algorithm needs to handle large exponentiation efficiently without overflow.
Special Values:
• Handling zero as an exponent (e.g., `x^0` should always return 1).
Constraints:
• Handle negative exponents by computing the reciprocal of the result.
double myPow(double x, int n) {
if(n== 0) return 1;
if(n < 0) return 1/x * myPow(1/x, -(n + 1));
return (n % 2) ? x * myPow(x * x, n / 2) : myPow(x * x, n/ 2);
}
1 : Function Declaration
double myPow(double x, int n) {
This line declares a function named `myPow` that takes a double `x` and an integer `n` as input and returns the result of `x` raised to the power of `n`.
2 : Base Case: Zero Power
if(n== 0) return 1;
This line checks if the exponent `n` is 0. If so, the function returns 1, as any number raised to the power of 0 is 1.
3 : Base Case: Negative Power
if(n < 0) return 1/x * myPow(1/x, -(n + 1));
This line handles the case where the exponent `n` is negative. It calculates the reciprocal of `x` and recursively calls the function with the absolute value of `n` incremented by 1.
4 : Recursive Calculation
return (n % 2) ? x * myPow(x * x, n / 2) : myPow(x * x, n/ 2);
This line implements the core recursive calculation. It leverages the fact that `x^n` can be calculated efficiently using the following properties:
1. **Even Power:** If `n` is even, `x^n = (x^2)^(n/2)`.
2. **Odd Power:** If `n` is odd, `x^n = x * (x^2)^((n-1)/2)`.
Best Case: O(log n)
Average Case: O(log n)
Worst Case: O(log n)
Description: The time complexity is logarithmic due to the halving of the exponent in each recursive step.
Best Case: O(1)
Worst Case: O(log n)
Description: The space complexity is logarithmic due to the recursive calls. In the best case (no recursion), it is constant.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus