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

鸿蒙金融理财全栈项目——生态合作、用户运营、数据变现

鸿蒙金融理财全栈项目——生态合作、用户运营、数据变现

《鸿蒙APP开发从入门到精通》第19篇:鸿蒙金融理财全栈项目——生态合作、用户运营、数据变现 📊🌍💰 内容承接与核心价值 这是《鸿蒙APP开发从入门到精通》的第19篇——生态合作、用户运营、数据变现篇,100%承接第18篇的风险控制、合规审计、产品创新架构,并基于金融场景的生态合作、用户运营、数据变现要求,设计并实现鸿蒙金融理财全栈项目的生态合作、用户运营、数据变现功能。 学习目标: * 掌握鸿蒙金融理财项目的生态合作设计与实现; * 实现金融机构合作、支付渠道合作、数据分析合作; * 理解用户运营在金融场景的核心设计与实现; * 实现用户增长、用户留存、用户转化; * 掌握数据变现在金融场景的设计与实现; * 实现数据服务、数据产品、数据变现; * 优化金融理财项目的用户体验(生态合作、用户运营、数据变现)。 学习重点: * 鸿蒙金融理财项目的生态合作设计原则; * 用户运营在金融场景的应用; * 数据变现在金融场景的设计要点。 一、 生态合作基础 🎯 1.1 生态合作定义 生态合作是指金融理财项目与其他金融机构、

By Ne0inhk
Flutter for OpenHarmony:cli_util 告别手写 print,用专业级日志系统构建你的 Dart 命令行工具 深度解析与鸿蒙适配指南

Flutter for OpenHarmony:cli_util 告别手写 print,用专业级日志系统构建你的 Dart 命令行工具 深度解析与鸿蒙适配指南

欢迎加入开源鸿蒙跨平台社区:https://openharmonycrossplatform.ZEEKLOG.net 前言 随着 Flutter 和 Dart 生态的爆发,越来越多的开发者开始使用 Dart 编写命令行工具(CLI)。从官方的 flutter 工具链,到社区的 melos、very_good_cli,Dart 因其 AOT 编译出的独立二进制文件(无需安装运行时)和极快的启动速度,已成为编写跨平台 CLI 的首选语言。 但在开发 CLI 时,我们经常面临一些底层痛点: * SDK 哪里找? 如何准确找到当前运行环境的 Dart SDK 路径?(用于调用 dart format 或 dart pub)。 * 日志怎么打? 简单的

By Ne0inhk
鸿蒙金融理财全栈项目——运维监控、性能优化、安全加固

鸿蒙金融理财全栈项目——运维监控、性能优化、安全加固

《鸿蒙APP开发从入门到精通》第20篇:鸿蒙金融理财全栈项目——运维监控、性能优化、安全加固 📊🔧🛡️ 内容承接与核心价值 这是《鸿蒙APP开发从入门到精通》的第20篇——运维监控、性能优化、安全加固篇,100%承接第19篇的生态合作、用户运营、数据变现架构,并基于金融场景的运维监控、性能优化、安全加固要求,设计并实现鸿蒙金融理财全栈项目的运维监控、性能优化、安全加固功能。 学习目标: * 掌握鸿蒙金融理财项目的运维监控设计与实现; * 实现应用监控、服务器监控、数据库监控; * 理解性能优化在金融场景的核心设计与实现; * 实现前端优化、后端优化、数据库优化; * 掌握安全加固在金融场景的设计与实现; * 实现代码加固、数据加密、安全审计; * 优化金融理财项目的用户体验(运维监控、性能优化、安全加固)。 学习重点: * 鸿蒙金融理财项目的运维监控设计原则; * 性能优化在金融场景的应用; * 安全加固在金融场景的设计要点。 一、 运维监控基础 🎯 1.1 运维监控定义 运维监控是指对金融理财项目的应用、

By Ne0inhk
国产化消息中间件双雄:东方通TongLINK/Q与华为RabbitMQ的运维核心技术全解析

国产化消息中间件双雄:东方通TongLINK/Q与华为RabbitMQ的运维核心技术全解析

在信创产业全面推进“2+8+N”替代工程的背景下,消息中间件作为分布式系统的“神经中枢”,承担着跨系统数据传输、应用解耦、流量削峰的核心使命,其稳定性、安全性与适配性直接决定政企数字化系统的运行效能。作为国产消息中间件领域的标杆产品,东方通TongLINK/Q凭借三十余年行业积淀的全场景适配能力,与华为RabbitMQ国产化适配版依托鲲鹏架构的高性能优化,成为政企信创改造的首选组合。 对于运维人员而言,熟练掌握两款产品的队列配置、消息路由管理与死信队列处理技术,是保障信创系统高效运转的关键。本文将深度拆解两大产品的核心运维技术,结合实操场景与行业案例,揭秘国产化消息中间件如何通过精细化配置实现业务价值最大化,为信创运维工作提供实战指南。 一、国产化适配基石:两款产品的核心定位与信创价值 消息中间件作为基础软件“三驾马车”之一,是信创生态建设的核心环节。东方通与华为凭借各自技术优势,构建了适配全场景信创需求的消息中间件解决方案,为政企系统替代升级筑牢根基。 (一)东方通TongLINK/Q:深耕行业的国产化消息中间件标杆 东方通TongLINK/Q作为国内最早自主研发的消息

By Ne0inhk