尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
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.
RELATED

相关推荐

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

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

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

📅 2026/10/19 15:44:38
Python进阶与并发编程14:boreal偶尔503,重试一次究竟要花掉谁的预算

Python进阶与并发编程14:boreal偶尔503,重试一次究竟要花掉谁的预算

/* 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 15:39:37
28:分离整数的各个数位

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 15:39:37
MORE NEWS

更多资讯

📰

词云AI如何驱动呼叫中心从资源驱动走向AI驱动

干呼叫中心这行的人,大多有一种很清晰的体感:过去拼的是人力规模、线路数量、排班密度,也就是俗称的资源驱动;但这两年,行业方向明显转向了AI驱动,尤其是词云AI这种“从对话文本里挖价值”的技术方案&#…

📰

内存乱序访问:高效CPU背后的并发陷阱与修复指南

如果你写过多线程代码,大概率遇到过这种诡异场景:代码里明明是先把一个指针初始化好,再把“就绪标志”置为true;可另一个线程却先看到标志已经是true,读指针时拿到的还是空值或半初始化状态。这不是编译器发疯&#xf…

📰

端侧多模态实时对话系统开发实战:9B模型的流式架构与性能调优

上个月我在做“某端侧多模态实时对话 Demo”的性能优化,项目代号模拟项目X,核心是把一个 9B 参数的视觉语言模型塞进手机,实现“摄像头看得到、麦克风听得见、离线也能聊”的交互体验。第一版跑通时大家都很兴奋,但真到演示环节就…

📰

工业级地图可视化大屏模板:支持GIS接口对接与动态刷新

简介:这是一份开箱即用的地图数据可视化大屏模板,面向数据分析师、前端开发人员及智慧城市项目实施者,解决地理信息在指挥中心、汇报展厅等大屏场景下的动态呈现与交互分析需求。压缩包共15个文件,含4个PNG图像资源(用…

📰

企业级AI视频管理平台源码级重构与低代码交付实战

1. 项目背景与整体方案拆解1.1 先看清楚这是一个什么体量的项目这个项目不是从零开始搭一套视频平台,而是在一套已经跑了一年多的企业级 AI 视频管理平台上做二次开发。原平台本身已经具备基础能力:视频上传、转码、切片、基础检索、人员管理&#xff0c…

📰

ISATAP隧道处理全解析:原理、配置与排障实战

1. 从一个让人抓狂的排障现场说起第一次接触ISATAP隧道,是在一个混合网络环境的联调现场。当时的情况大概是这样:办公网是纯IPv4的,但实验室那边新上了一批只跑IPv6的测试设备,两边要互通,中间又不可能为了这个需求把整…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬