Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Minimize Result by Adding Parentheses to Expression

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2232" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a 0-indexed string expression of the form "+" where and represent positive integers. Add a pair of parentheses to expression such that after the addition of parentheses, expression is a valid mathematical expression and evaluates to the smallest possible value. The left parenthesis must be added to the left of '+' and the right parenthesis must be added to the right of '+'. Return expression after adding a pair of parentheses such that expression evaluates to the smallest possible value. If there are multiple answers that yield the same result, return any of them. The input has been generated such that the original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer. Example 1: Input: expression = "247+38" Output: "2(47+38)" Explanation: The expression evaluates to 2 (47 + 38) = 2 85 = 170. Note that "2(4)7+38" is invalid because the right parenthesis must be to the right of the '+'. It can be shown that 170 is the smallest possible value. Example 2: Input: expression = "12+34" Output: "1(2+3)4" Explanation: The expression evaluates to 1 (2 + 3) 4 = 1 5 4 = 20. Example 3: Input: expression = "999+999" Output: "(999+999)" Explanation: The expression evaluates to 999 + 999 = 1998. Constraints: 3 <= expression.length <= 10 expression consists of digits from '1' to '9' and '+'. expression starts and ends with digits. expression contains exactly one '+'. The original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.

Explanation

  • Identify the numbers: Split the input string into two numbers based on the '+' operator.
    • Determine placement of parentheses: Evaluate the expression with parentheses placed at all valid locations (left of '+' and right of '+').
    • Find minimum value: Return the expression string with the placement of parentheses that yields the smallest value.
  • Runtime Complexity: O(1), Storage Complexity: O(1) (due to the limited input size)

Code

    def minimizeResult(expression: str) -> str:
    """
    Given a string expression of the form "<num1>+<num2>", adds parentheses to minimize the result.

    Args:
        expression: The input string expression.

    Returns:
        The expression with parentheses added for the smallest possible value.
    """
    plus_index = expression.find('+')
    num1 = expression[:plus_index]
    num2 = expression[plus_index + 1:]
    n1_len = len(num1)
    n2_len = len(num2)

    min_val = float('inf')
    best_expr = ""

    for i in range(n1_len):
        for j in range(n2_len):
            left_num = num1[:i] if i > 0 else 1
            inner_left = num1[i:]
            inner_right = num2[:j+1]
            right_num = num2[j+1:] if j+1 < n2_len else 1

            if isinstance(left_num, str):
                left_num = int(left_num)

            if isinstance(right_num, str):
                right_num = int(right_num)

            val = (int(inner_left) + int(inner_right))

            if isinstance(left_num, int):
                val = val * left_num

            if isinstance(right_num, int):
                val = val * right_num


            if val < min_val:
                min_val = val
                best_expr = num1[:i] + "(" + num1[i:] + "+" + num2[:j+1] + ")" + num2[j+1:]

    return best_expr

More from this blog

C

Chatmagic blog

2894 posts