LeetCode //C - 964. Least Operators to Express Number

964. Least Operators to Express Number

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 < = t a r g e t < = 2 ∗ 10 8 1 <= target <= 2 * 10^8 1<=target<=2∗108

From: LeetCode

Link: 964. Least Operators to Express Number


Solution:

Ideas:
  • For small values v <= x, directly compute the best using only 1s (x/x) or as x minus some 1s.

  • For larger values, find the closest power x^k and try:

    • Undershoot: use x^(k-1) and build the remainder.
    • Overshoot: use x^k and subtract the difference (only if it helps).
  • Use memoization to avoid recomputing subproblems.

Code:
c 复制代码
typedef struct {
    int key;
    int val;
} Pair;

static Pair memo[10000];
static int memoSize;

static int dfs(int x, int v) {
    // Base case: v <= x
    if (v <= x) {
        // Option 1: v = 1 + 1 + ... + 1 (v times)
        //   1 is x / x  -> 1 operator
        //   plus (v - 1) additions
        //   total = v (division) + (v - 1) (+) = 2*v - 1
        int op_add = 2 * v - 1;

        // Option 2: v = x - ( (x - v) ones )
        //   (x - v) ones: 2*(x - v) - 1 operators
        //   one more '-' to subtract from x
        //   total = 2*(x - v)
        int op_sub = 2 * (x - v);

        return op_add < op_sub ? op_add : op_sub;
    }

    // Check memo
    for (int i = 0; i < memoSize; ++i) {
        if (memo[i].key == v) return memo[i].val;
    }

    // Find smallest k such that x^k >= v
    int k = 2;
    long y = (long)x * x;   // y = x^2 initially
    while (y < v) {
        y *= x;
        ++k;               // now y = x^k
    }
    // Now y = x^k >= v, and y/x = x^(k-1)

    // Option 1 (undershoot):
    //   Use x^(k-1) once (cost k-1 multiplications),
    //   then express the remaining (v - x^(k-1))
    int op1 = (k - 1) + dfs(x, v - (int)(y / x));

    int ans = op1;

    // Option 2 (overshoot), only if the "over" part is smaller than v:
    //   Use x^k once (cost k multiplications),
    //   then express (x^k - v), and subtract it.
    if (y - v < v) {
        int op2 = k + dfs(x, (int)(y - v));
        if (op2 < ans) ans = op2;
    }

    // Save to memo
    memo[memoSize].key = v;
    memo[memoSize].val = ans;
    ++memoSize;

    return ans;
}

int leastOpsExpressTarget(int x, int target) {
    memoSize = 0;  // reset memo for each test case
    return dfs(x, target);
}
相关推荐
三佛科技-18736613397几秒前
LP3783A芯茂微5V2.1A低功耗原边反馈充电器芯片替代PL3378/C
c语言·开发语言
ArturiaZ14 分钟前
【day55】
数据结构·c++·算法
仰泳的熊猫17 分钟前
题目2279:蓝桥杯2018年第九届真题-日志统计
数据结构·c++·算法·蓝桥杯
一叶落43818 分钟前
LeetCode 11:盛最多水的容器(C语言实现)
c语言·数据结构·算法·leetcode
Emilin Amy20 分钟前
一台具备了“观察力”的下肢康复外骨骼机器人
算法·机器人
I_LPL20 分钟前
day53 代码随想录算法训练营 图论专题7
算法·图论
_日拱一卒22 分钟前
LeetCode(力扣):二叉树的后序遍历
算法·leetcode·职场和发展
Emilin Amy24 分钟前
【ROS】机器人的速度/角度/力矩控制是如何实现的
c++·算法·控制·ros1/2
sali-tec28 分钟前
C# 基于OpenCv的视觉工作流-章36-骨架提取
图像处理·人工智能·opencv·算法·计算机视觉
CoovallyAIHub29 分钟前
RF-DETR:最近一个月迭代 5 个版本的实时检测+分割模型
深度学习·算法·计算机视觉