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.
We can represent the target as a sum of powers of x, and recursively try to build the target using the least number of operators. At each step, we can either add or subtract the closest power of x, and use memoization to avoid recomputation.
classSolution {
public: unordered_map<int, int> memo;
int x;
intleastOpsExpressTarget(int X, int target) {
x = X;
return dfs(target);
}
intdfs(int t) {
if (t ==0) return0;
if (t < x) return min(2* t -1, 2* (x - t));
if (memo.count(t)) return memo[t];
int k =0, p =1;
while (p * x <= t) {
p *= x;
++k;
}
int left = dfs(t - p) + k;
int right = dfs(p * x - t) + k +1;
return memo[t] = min(left, right);
}
};
classSolution {
Map<Integer, Integer> memo =new HashMap<>();
int x;
publicintleastOpsExpressTarget(int X, int target) {
x = X;
return dfs(target);
}
intdfs(int t) {
if (t == 0) return 0;
if (t < x) return Math.min(2 * t - 1, 2 * (x - t));
if (memo.containsKey(t)) return memo.get(t);
int k = 0, p = 1;
while (p * x <= t) {
p *= x;
k++;
}
int left = dfs(t - p) + k;
int right = dfs(p * x - t) + k + 1;
int ans = Math.min(left, right);
memo.put(t, ans);
return ans;
}
}
classSolution {
val memo = mutableMapOf<Int, Int>()
var x = 0funleastOpsExpressTarget(X: Int, target: Int): Int {
x = X
return dfs(target)
}
fundfs(t: Int): Int {
if (t ==0) return0if (t < x) return minOf(2 * t - 1, 2 * (x - t))
memo[t]?.let { returnit }
var k = 0; var p = 1while (p * x <= t) {
p *= x; k++ }
val left = dfs(t - p) + k
val right = dfs(p * x - t) + k + 1val ans = minOf(left, right)
memo[t] = ans
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
defleastOpsExpressTarget(x: int, target: int) -> int:
memo: dict[int, int] = {}
defdfs(t: int) -> int:
if t ==0:
return0if t < x:
return min(2* t -1, 2* (x - t))
if t in memo:
return memo[t]
k, p =0, 1while p * x <= t:
p *= x
k +=1 left = dfs(t - p) + k
right = dfs(p * x - t) + k +1 memo[t] = min(left, right)
return memo[t]
return dfs(target)
impl Solution {
pubfnleast_ops_express_target(x: i32, target: i32) -> i32 {
use std::collections::HashMap;
fndfs(x: i32, t: i32, memo: &mut HashMap<i32, i32>) -> i32 {
if t ==0 { return0; }
if t < x { return (2* t -1).min(2* (x - t)); }
iflet Some(&v) = memo.get(&t) { return v; }
letmut k =0; letmut p =1;
while p * x <= t {
p *= x; k +=1;
}
let left = dfs(x, t - p, memo) + k;
let right = dfs(x, p * x - t, memo) + k +1;
let ans = left.min(right);
memo.insert(t, ans);
ans
}
letmut memo = HashMap::new();
dfs(x, target, &mut memo)
}
}