{x}
blog image

Build the Equation

Solution Explanation for LeetCode Problem 2118: Build the Equation

This problem involves constructing a mathematical equation from data in a table. The equation must be in a specific format, with terms sorted by power in descending order and the right-hand side equal to zero.

Approach

The solution uses a SQL query to achieve this. The core idea is to process the Terms table in several steps:

  1. Format each term: A CASE statement within a Common Table Expression (CTE) called T handles the formatting of each term based on its power. It determines the sign ('+' or '-') and appends 'X' and '^power' appropriately according to the problem's specifications.

  2. Concatenate terms: The GROUP_CONCAT function combines the formatted terms from the T CTE. The ORDER BY power DESC clause ensures the terms are sorted by descending power, and SEPARATOR '' avoids adding extra characters between terms.

  3. Append "=0": Finally, the CONCAT function adds "=0" to complete the equation.

MySQL Code Explanation

WITH
    T AS (
        SELECT
            power,
            CASE power
                WHEN 0 THEN IF(factor > 0, CONCAT('+', factor), factor)
                WHEN 1 THEN CONCAT(
                    IF(factor > 0, CONCAT('+', factor), factor),
                    'X'
                )
                ELSE CONCAT(
                    IF(factor > 0, CONCAT('+', factor), factor),
                    'X^',
                    power
                )
            END AS it
        FROM Terms
    )
SELECT
    CONCAT(GROUP_CONCAT(it ORDER BY power DESC SEPARATOR ""), '=0') AS equation
FROM T;
  • WITH T AS (...): This defines a CTE named T. CTEs are temporary named result sets that can be referenced within a single query. This improves readability and organization.

  • SELECT power, CASE ... END AS it FROM Terms: This selects the power and generates the formatted term (it). The CASE statement handles the three scenarios: power = 0, power = 1, and power > 1. IF(factor > 0, CONCAT('+', factor), factor) dynamically adds a '+' sign if the factor is positive, otherwise it uses the factor as is (negative).

  • SELECT CONCAT(GROUP_CONCAT(it ...), '=0') AS equation FROM T: This is the final selection. GROUP_CONCAT(it ORDER BY power DESC SEPARATOR "") concatenates all formatted terms from CTE T, sorting them by power in descending order. CONCAT adds "=0" to create the final equation string.

Time Complexity Analysis

The time complexity of this SQL query is dominated by the sorting step within GROUP_CONCAT. The sorting step has a time complexity of O(N log N), where N is the number of rows in the Terms table. The other operations (formatting, concatenation) have linear time complexity, O(N). Therefore, the overall time complexity is O(N log N). The space complexity is O(N) to store the intermediate results.

Follow-up: Non-Primary Key power

If power were not a primary key and uniqueness was required, a slight modification would be needed. We could add a DISTINCT keyword to the CTE to select only unique powers before formatting and concatenation. This would not significantly change the time complexity, as the DISTINCT operation's complexity depends on the distribution of data but generally remains within O(N log N) or O(N) for efficient implementations. The space complexity would remain O(N).