Least Operators To Express Number Problem
Description
LeetCode Problem 964.
Given a single positive integer x, we will write an expression of the form x (op1) x (op2) x (op3) x ... where each operator op1, op2, etc. is either addition, subtraction, multiplication, or division (+, -, *, or /). For example, with x = 3, we might write 3 * 3 / 3 + 3 - 3 which is a value of 3.
When writing such an expression, we adhere to the following conventions:
- The division operator (/) returns rational numbers.
- There are no parentheses placed anywhere.
- We use the usual order of operations: multiplication and division happen before addition and subtraction.
- It is not allowed to use the unary negation operator (-). For example, "x - x" is a valid expression as it only uses subtraction, but "-x + x" is not because it uses negation.
We would like to write an expression with the least number of operators such that the expression equals the given target. Return the least number of operators used.
Example 1:
Input: x = 3, target = 19
Output: 5
Explanation: 3 * 3 + 3 * 3 + 3 / 3.
The expression contains 5 operations.
Example 2:
Input: x = 5, target = 501
Output: 8
Explanation: 5 * 5 * 5 * 5 - 5 * 5 * 5 + 5 / 5.
The expression contains 8 operations.
Example 3:
Input: x = 100, target = 100000000
Output: 3
Explanation: 100 * 100 * 100 * 100.
The expression contains 3 operations.
Constraints:
- 2 <= x <= 100
- 1 <= target <= 2 * 10^8
Solution
Approach: Digit DP in Base x
Without parentheses, the expression is a list of terms joined by + and -, where each term is a run of xs joined by * and /. The terms worth using are the powers of x, and each has a fixed price, counting the + or - in front of it:
x^0 = x / xcosts 2 operators (the/and the sign).x^kfork >= 1isx * x * ... * x, which costsk(k - 1multiplications and the sign).
The first term has no sign in front, so the answer is the total price minus 1. The task becomes: write target as a sum of ±x^k terms with the smallest total price.
Read target in base x, from the lowest digit up. A digit d at position k can be reached in two ways:
- Add
dcopies ofx^k. - Overshoot: subtract
x - dcopies ofx^kand carry 1 into positionk + 1.
So keep two numbers while scanning, where low is the value of the digits seen so far:
pos: the fewest operators that make exactlylow.neg: the fewest operators that makex^(k+1) - low, for when the next position takes the carry and subtracts this part.
At position 0 nothing lies below, so pos = 2d and neg = 2(x - d). At each later position k, with digit d and cost = k:
pos = min(d · cost + pos, (d + 1) · cost + neg): adddcopies on top oflow, or addd + 1copies and subtract the overshoot part.neg = min((x - d) · cost + pos, (x - d - 1) · cost + neg): the same choice for the complement.
Compute both new values from the old pair before assigning either. After the top digit, the overshoot still needs one more term x^k costing k (the number of digits), so the answer is min(pos, k + neg) - 1.
For x = 3, target = 19 (base 3: 201): the best is 3 * 3 + 3 * 3 + 3 / 3, prices 2 + 2 + 2 minus 1, which gives 5.
Complexity: O(logx target) time, one step per base-x digit, and O(1) space.
class Solution:
def leastOpsExpressTarget(self, x: int, target: int) -> int:
# Read target in base x, one digit at a time from the lowest.
# pos: fewest operators to make the low part of target seen so far exactly.
# neg: fewest operators to make x^power minus that part (we overshoot with one
# more x^power later and subtract it).
pos = neg = 0
power = 0
while target > 0:
target, digit = divmod(target, x)
if power == 0:
# x^0 is x / x: 2 operators, counting the + or - in front of the term.
pos, neg = 2 * digit, 2 * (x - digit)
else:
# x^power is x * x * ... * x: power - 1 operators, plus the + or - in front.
cost = power
new_pos = min(digit * cost + pos, (digit + 1) * cost + neg)
new_neg = min((x - digit) * cost + pos, (x - digit - 1) * cost + neg)
pos, neg = new_pos, new_neg
power += 1
# Overshooting needs one more term x^power (cost power).
# The first term of the expression has no sign in front of it, hence the -1.
return min(pos, power + neg) - 1
Now write it from memory
Close the write-up and solve it yourself against the tests.