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

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:
typedefstruct{int key;int val;} Pair;static Pair memo[10000];staticint memoSize;staticintdfs(int x,int v){// Base case: v <= xif(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 - 1int 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 memofor(int i =0; i < memoSize;++i){if(memo[i].key == v)return memo[i].val;}// Find smallest k such that x^k >= vint k =2;long y =(long)x * x;// y = x^2 initiallywhile(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;}intleastOpsExpressTarget(int x,int target){ memoSize =0;// reset memo for each test casereturndfs(x, target);}

Read more

A / B测试太慢?AI帮你实时优化实验策略

A / B测试太慢?AI帮你实时优化实验策略

👋 大家好,欢迎来到我的技术博客! 📚 在这里,我会分享学习笔记、实战经验与技术思考,力求用简单的方式讲清楚复杂的问题。 🎯 本文将围绕AI这个话题展开,希望能为你带来一些启发或实用的参考。 🌱 无论你是刚入门的新手,还是正在进阶的开发者,希望你都能有所收获! 文章目录 * A/B测试太慢?AI帮你实时优化实验策略 🚀 * 为什么传统A/B测试成了效率黑洞? * AI驱动的实时优化:从“被动等待”到“主动决策” * 贝叶斯优化:AI决策的数学引擎 * 代理模型:预测点击率 * 采集函数:决定下一步策略 * 代码实战:用Python实现AI优化A/B测试 * 代码执行结果示例 * 实时决策流程:AI如何动态调整实验? * 实际业务场景:电商大促的AI优化案例 * 贝叶斯优化 vs 其他AI方法 * 如何在你的系统中落地AI优化? * 步骤1:构建基础数据层 * 步骤2:集成AI优化引擎 * 步骤3:设置停止条件 * 为什么AI优化能避免“实验陷阱”?

By Ne0inhk
人工智能:自然语言处理在教育领域的应用与实战

人工智能:自然语言处理在教育领域的应用与实战

人工智能:自然语言处理在教育领域的应用与实战 学习目标 💡 理解自然语言处理(NLP)在教育领域的应用场景和重要性 💡 掌握教育领域NLP应用的核心技术(如智能问答、作业批改、个性化学习) 💡 学会使用前沿模型(如BERT、GPT-3)进行教育文本分析 💡 理解教育领域的特殊挑战(如多学科知识、学生认知差异、数据隐私) 💡 通过实战项目,开发一个智能问答系统应用 重点内容 * 教育领域NLP应用的主要场景 * 核心技术(智能问答、作业批改、个性化学习) * 前沿模型(BERT、GPT-3)在教育领域的使用 * 教育领域的特殊挑战 * 实战项目:智能问答系统应用开发 一、教育领域NLP应用的主要场景 1.1 智能问答 1.1.1 智能问答的基本概念 智能问答是通过自然语言与用户进行交互,回答用户问题的程序。在教育领域,智能问答的主要应用场景包括: * 课程问答:回答课程相关的问题(如“什么是机器学习”

By Ne0inhk
AI工具泛滥时代,为什么“能力“越来越不值钱?

AI工具泛滥时代,为什么“能力“越来越不值钱?

文章目录 * 一、一个荒诞的现象:工具民主化与机会不平等 * 二、三个被误读的AI创业神话 * 三、AI创作者的真正壁垒:从"工具使用者"到"商业闭环构建者" * 四、给新手的实战建议:从0到1的行动清单 * 五、关于《脉向AI》栏目 * 六、适合谁看? 一、一个荒诞的现象:工具民主化与机会不平等 2025被称为"AI应用元年",但一个诡异的分化正在发生。 一方面,AI工具从未如此普及。ChatGPT、Midjourney、Claude、Sora、可灵、即梦……每个月都有新的"生产力神器"登上热搜。知识付费市场上,“AI副业课”" prompt工程&

By Ne0inhk
人工智能:自然语言处理在法律领域的应用与实战

人工智能:自然语言处理在法律领域的应用与实战

自然语言处理在法律领域的应用与实战 学习目标 💡 理解自然语言处理(NLP)在法律领域的应用场景和重要性 💡 掌握法律领域NLP应用的核心技术(如法律文本分类、实体识别、合同分析) 💡 学会使用前沿模型(如LegalBERT、LexGLUE)进行法律文本分析 💡 理解法律领域的特殊挑战(如专业术语、法律规范、数据稀缺) 💡 通过实战项目,开发一个合同分析应用 重点内容 * 法律领域NLP应用的主要场景 * 核心技术(法律文本分类、实体识别、合同分析) * 前沿模型(LegalBERT、LexGLUE)在法律领域的使用 * 法律领域的特殊挑战 * 实战项目:合同分析应用开发 一、法律领域NLP应用的主要场景 1.1 法律文本分类 1.1.1 法律文本分类的基本概念 法律文本分类是将法律文本划分到预定义类别的过程。在法律领域,法律文本分类的主要应用场景包括: * 判例分类:将判例分为不同的类别(如民事、刑事、行政) * 法律文件分类:

By Ne0inhk