DeepSeek LeetCode 668. Kth Smallest Number in Multiplication Table Java Implement

发布时间:2026/10/19 15:44:53

DeepSeek    LeetCode 668. Kth Smallest Number in Multiplication Table Java Implement Here is the Java implementation for LeetCode 668. Kth Smallest Number in Multiplication Table, using the standard Binary Search on Value approach.Core IdeaThe multiplication table is far too large to materialize (up to 9 \times 10^8 cells). Instead of finding the k-th number directly, we solve a reverse problem:Given a value x, how many entries in the table are less than or equal to x?This count is easy to compute:· In the i-th row, the entries are i1, i2, …, i*n. The number of entries ≤ x is simply min(x / i, n).· Summing this over all rows gives count(x).The function count(x) is monotonically non-decreasing as x increases. Therefore, we can binary search for the smallest x such that count(x) k. This x is guaranteed to be the k-th smallest number in the table.Java Implementation (Standard Binary Search)classSolution{publicintfindKthNumber(intm,intn,intk){intlo1;inthim*n;while(lohi){intmidlo(hi-lo)/2;intcount0;// Count how many entries are midfor(inti1;im;i){countMath.min(mid/i,n);// Early exit if we already have enoughif(countk)break;}if(countk){himid;// mid could be the answer; search left}else{lomid1;// mid is too small; search right}}returnlo;}}This code is adapted from the standard solution provided in the LeetCode community.Complexity AnalysisMetric Complexity ExplanationTime O(m \log(m \cdot n)) Binary search takes O(\log(m \cdot n)) iterations, and each iteration scans up to m rows.Space O(1) Only a few integer variables are used.Possible Optimization (Optional)The standard loop iterates over all m rows. However, for rows where i \le \lfloor mid / n \rfloor, every entry in that row is \le mid. This creates a “saturated block” of rows where count is simply n per row.We can sum these rows in O(1) using multiplication, and only loop over the remaining rows. This reduces the inner loop from O(m) to O(\sqrt{mid}), bringing the total time down to O(\sqrt{m \cdot n} \log(m \cdot n)). For the given constraints, the standard version is already sufficient.Key Note: Use long for the count variable if you are concerned about integer overflow, although for m, n ≤ 30,000, the maximum total count (9 × 10^8) still fits within a 32-bit signed integer. The mid variable also fits in int.
延伸阅读

更多相关文章

2026/10/19 15:44:53

open-code-review:代码审查自动化工具的设计与实战解析

搞代码审查这么多年,我一直觉得这活儿本身才是团队里最需要被“审查”的一环。每次提到 code review,老程序员想到的是又一轮口水战,新程序员想到的是提心吊胆的“批斗会”,管理层看到的是进度表上永远悬着的那根刺。直到我动手把…

2026/10/19 15:44:53

超长文本语义分析:基于百万token窗口的‘撒网法’全解析

网撒得开,鱼才进得来;问提得准,理才出得去。两者之间,隔着一整片文本的海。最近一直在处理一部约280万字的长篇书稿,做语义层面的整体分析。这个体量刚好超出单个百万token窗口的覆盖范围,只能拆成三个百万…

2026/10/19 15:39:53

28:分离整数的各个数位

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/19 16:45:13

open-code-review:基于 Git Diff 的 CLI 代码审查新范式

1. 这不是另一个“代码审查工具”,而是一次对协作本质的重新定义“open-code-review”这五个字母组合,乍看像某个开源项目仓库名,但真正把它敲进终端、跑起来、用上手之后,我才意识到——它根本不是传统意义的 code review 工具。…

2026/10/19 16:45:13

专科生毕业论文救星:8款AI论文写作软件全流程拆解

每年到了毕业季,问“学长有没有靠谱的AI论文写作软件”的人就特别多。我自己当年也是专科毕业,写毕业论文那会儿是真的慌:不会选题、不会查文献、更不知道大纲怎么搭,最后硬着头皮把几十个工具挨个试了一遍,沉淀下来真…

2026/10/19 16:45:13

手推RNN与LSTM反向传播:从公式到代码的完整指南

1. 为什么值得亲手推一遍RNN和LSTM的反向传播刚接触序列模型那会儿,我和很多人一样,觉得反向传播这事儿调包就完了——框架自动求导,一行loss.backward()搞定,何必手推?直到有一次训练一个文本分类的小模型&#xff0c…

2026/10/19 16:45:13

工业AI项目敏捷开发落地:以可验证模型交付为核心

做了几年工业AI项目的落地,我一听到“敏捷开发”四个字,第一反应不是白板、站会、冲刺,而是“这个迭代到底交付了什么可验证的东西”。工业AI项目和互联网产品有一个本质区别:代码写完不算数,模型训练完也不算数&#…

2026/10/19 16:40:12

AI模型量化精度控制实战:误差溯源与混合精度分配

一次部署项目里,我把一个对话模型从 FP16 压到 INT8,任务准确率掉了 2 个多百分点。第一反应是量化位宽不够,换成更复杂的量化方法之后,精度没回来,反而把推理速度拖慢了。后来排查到根因,才发现问题根本不…

2026/10/18 0:01:50

金融AI落地指南:从调研报告到技术选型与工程实践

简介:这份PDF报告聚焦2025年金融服务业中国AI现状与趋势,面向金融科技从业者、AI战略规划人员及关注行业数字化转型的研究者,帮助读者把握生成式AI在银行、支付、交易等场景中的落地路径与投资方向。资源包内含1个PDF文件,大小约1…

2026/10/19 0:04:56

悟空开源无代码平台实战:从实体建模到应用设计避坑指南

简介:基于悟空开源无代码平台的多种应用设计源码,面向希望在不编写代码的情况下快速搭建CRM、HRM、财务系统、ERP、SRM等业务应用的企业与技术人员,通过拖放式界面和内置模板即可完成应用定制与部署。压缩包共697个文件,约10.74MB…

2026/10/19 0:04:56

基于.NET9的多租户权限系统:数据权限与动态API落地实践

简介:这份资源是一套面向 .NET 与 Vue 全栈开发者的中台权限管理系统,后端基于 .Net9,前端采用 Vue3 Element Plus,并配套 uniapp uviewui 移动端方案,实现前后端分离。系统覆盖多租户隔离、数据权限、动态 Api、任务…

2026/10/19 0:04:56

场地预约小程序毕设实战:SSM+微信小程序+MySQL数据库设计到避坑

简介:面向毕业设计场景的场地预约小程序完整项目,基于微信小程序SSMMySql技术栈开发,适合计算机相关专业学生作为选题参考、功能扩展或二次开发起点。系统实现场地类型管理、场地管理、预约管理、取消申请与押金退回等功能,分为管…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑