964. Least Operators to Express Number Hard

@problem@discussion
#Math#Dynamic Programming



1/**
2 * [964] Least Operators to Express Number
3 *
4 * 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 <font face="monospace">3</font>.
5 * When writing such an expression, we adhere to the following conventions:
6 * 
7 * 	The division operator (/) returns rational numbers.
8 * 	There are no parentheses placed anywhere.
9 * 	We use the usual order of operations: multiplication and division happen before addition and subtraction.
10 * 	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.
11 * 
12 * 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.
13 *  
14 * Example 1:
15 * 
16 * Input: x = 3, target = 19
17 * Output: 5
18 * Explanation: 3 * 3 + 3 * 3 + 3 / 3.
19 * The expression contains 5 operations.
20 * 
21 * Example 2:
22 * 
23 * Input: x = 5, target = 501
24 * Output: 8
25 * Explanation: 5 * 5 * 5 * 5 - 5 * 5 * 5 + 5 / 5.
26 * The expression contains 8 operations.
27 * 
28 * Example 3:
29 * 
30 * Input: x = 100, target = 100000000
31 * Output: 3
32 * Explanation: 100 * 100 * 100 * 100.
33 * The expression contains 3 operations.
34 * 
35 *  
36 * Constraints:
37 * 
38 * 	2 <= x <= 100
39 * 	1 <= target <= 2 * 10^8
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/least-operators-to-express-number/
45// discuss: https://leetcode.com/problems/least-operators-to-express-number/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn least_ops_express_target(x: i32, target: i32) -> i32 {
51        0
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_964() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.