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

Claude AI注册避坑指南:5分钟搞定海外手机号验证(附最新解决方案)

Claude AI 注册实战:从验证难题到高效上手的完整路径 最近几个月,身边不少朋友和同事都在讨论一个现象:想体验一下那个以“安全”和“长上下文”著称的Claude AI,却在注册的第一步——手机号验证——就卡住了。这确实是个挺让人头疼的体验,明明技术产品就在眼前,却因为一个看似简单的步骤而无法触及。对于国内的开发者、产品经理或是AI爱好者来说,这种“看得见却用不上”的感觉尤其强烈。这篇文章,就是为你准备的。我们不谈空泛的理论,只聚焦于一个核心目标:如何绕过那些常见的障碍,顺利、安全地完成Claude账户的注册与初步设置,并为你梳理清楚后续高效使用的关键点。整个过程,力求在5分钟内给你一个清晰的行动路线。 1. 理解注册流程的核心关卡与常见误区 在动手操作之前,我们先花点时间拆解一下Claude的注册流程,特别是那个让很多人“折戟”的环节。这能帮你避开很多不必要的试错,直接找到有效的路径。 Claude的官方注册流程,本质上和大多数国际主流互联网服务类似:邮箱验证 -> 手机号验证 ->

By Ne0inhk
Spring Cloud+AI :实现分布式智能推荐系统

Spring Cloud+AI :实现分布式智能推荐系统

欢迎文末添加好友交流,共同进步! “ 俺はモンキー・D・ルフィ。海贼王になる男だ!” 引言 * 在当今数字化时代,推荐系统已成为电商平台、内容分发平台、社交网络等互联网产品的核心竞争力之一。从淘宝的"猜你喜欢"、抖音的精准内容推送,到 Netflix 的影视推荐,优秀的推荐系统不仅能显著提升用户留存率和转化率,更能为企业带来可观的商业价值。据统计,亚马逊约 35% 的销售额来自推荐系统,Netflix 则通过推荐算法为用户节省了每年约 10 亿美元的搜索成本。 * 然而,随着业务规模的增长和推荐算法的复杂化,传统的单体架构逐渐暴露出诸多瓶颈。首先,推荐系统涉及用户画像构建、实时行为收集、特征工程、模型推理等多个环节,单体应用难以应对日益复杂的业务逻辑;其次,推荐服务需要处理海量并发请求,单机部署无法满足弹性伸缩的需求;再者,AI 模型的迭代更新日益频繁,单体架构下模型部署往往需要重启整个应用,严重影响线上服务稳定性;最后,企业需要支持 A/B

By Ne0inhk
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