力扣668.乘法表中第k小的数

力扣668.乘法表中第k小的数

  • 二分查找

    • 是否有k个比mid小的数
cpp 复制代码
  class Solution {
  public:
      int findKthNumber(int m, int n, int k) {
          auto check = [&](int mid) -> bool
          {
              int res=0;
              int row = 1,col = n;
              while(row <= m)
              {
                  if(row * col <= mid)
                  {
                      res += col;
                      if(res >= k) return false;
                      row ++;
                  }
                  else col --;
              }
              return true;
          };
          int l = 1,r = n * m;
          while(l<r)
          {
              int mid = l + r + 1 >> 1;
              if(check(mid)) l = mid;
              else r = mid - 1;
          }
          return r + 1;
      }
  };
相关推荐
超级大只老咪3 小时前
数组相邻元素比较的循环条件(Java竞赛考点)
java
hh随便起个名3 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
小浣熊熊熊熊熊熊熊丶3 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
毕设源码-钟学长3 小时前
【开题答辩全过程】以 公交管理系统为例,包含答辩的问题和答案
java·eclipse
啃火龙果的兔子3 小时前
JDK 安装配置
java·开发语言
星哥说事3 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
派大鑫wink4 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
xUxIAOrUIII4 小时前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home4 小时前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法
醇氧4 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea