KatInterview

Smallest Fully Bracketed Arithmetic Expression Hitting a Target

Dynamic ProgrammingAuto-graded · 31 test cases
MediumAuto-gradedDynamic Programmingrecursioninterval-dpexpression-trees

Last updated

Enumerate all operator and bracket choices over an ordered list of numbers and return the ASCII-smallest canonical expression that evaluates to the target, or an empty string.

You are given a list of non-negative integers values and an integer goal. The numbers must stay in the order given. Between each pair of neighbouring numbers you must place exactly one of the operators +, - or *, and you may group sub-expressions with parentheses in any way.

Your task is to find an expression that evaluates to goal. If several do, report the smallest one under the ordering defined below. If none do, report an empty string.

Rules

  • Each number is used exactly once and in its original position. You may not reorder numbers, glue them into multi-digit numbers, or apply a unary minus.
  • Exactly one operator sits between each pair of adjacent numbers. Division is not available. All arithmetic is exact integer arithmetic.
  • Expressions are written in canonical form:
    • A lone number is written as its decimal digits.
    • Every binary operation is written as ( + left part + operator + right part + ), with no spaces. This includes the outermost operation.
    • An expression over k numbers therefore contains exactly k - 1 pairs of parentheses.
    • Two different groupings are different strings, even if they have the same value.
  • Among all canonical expressions whose value equals goal, return the one that is smallest when compared character by character using ASCII codes. In this order ( < ) < * < + < - < 0 < 1 < ... < 9.
  • If no canonical expression evaluates to goal, return "".

Constraints

  • 1 <= len(values) <= 6
  • 0 <= values[i] <= 50
  • -10^11 <= goal <= 10^11
  • Every intermediate and final value of every expression lies in [-10^11, 10^11].
  • If len(values) == 1, the only expression is the number itself (no parentheses).

Examples

Example 1

Input: smallest_expression_for_goal([2, 3, 4], 14)

Output: '(2*(3+4))'

Example 2

Input: smallest_expression_for_goal([8, 3, 5, 2], 1)

Output: '((8+3)-(5*2))'

Example 3

Input: smallest_expression_for_goal([3, 3, 8], 3)

Output: ''

Hints

Hint 1

Choosing the parentheses is the same as choosing the last operation to be applied: it splits the list into a left block and a right block, each of which is an expression of the same kind.

Hint 2

For every contiguous interval of the list, compute all pairs (canonical string, value). Combine every split point, every left result, every right result and each of the three operators. Memoize per interval.

Hint 3

With at most 6 numbers there are only 42 * 3^5 = 10,206 expressions, so you can enumerate everything. Do not stop at the first match: collect every expression equal to the goal and keep the minimum string; Python string comparison already follows ASCII order.