#964
Least Operators to Express Number
candidate master · 1480 · lc hard +32 · verified · 48.7% accepted · 328 likes · top 35%
Description
Build an arithmetic expression using only copies of positive integer x joined by +, -, *, / — no parentheses, no unary negation, standard precedence — that equals target. Return the minimum number of operator symbols required.
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.
Code
1
2
3