# Code language: Python classSolution: deffindKthNumber(self, m: int, n: int, k: int) -> int: if m > n: returnself.findKthNumber(n, m, k) left, right = 1, n * m defcheck(x): returnsum(min(n, x // row) for row inrange(1, m + 1)) while left < right: mid = (left + right) >> 1 if check(mid) < k: left = mid + 1 else: right = mid return left
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Code language: Java classSolution { publicintfindKthNumber(int m, int n, int k) { if (m > n) return findKthNumber(n, m, k); intleft=1, right = n * m; // 最大 9e8 while (left < right) { intmid= left + (right - left) / 2; if (check(m, n, k, mid)) left = mid + 1; else right = mid; } return left; }
publicbooleancheck(int m, int n, int k, int x) { intcnt=0; for (inti=1; i <= m; ++i) cnt += Math.min(n, x / i); return cnt < k; } }
// Code language: Cpp classSolution { public: intfindKthNumber(int m, int n, int k){ if (m > n) returnfindKthNumber(n, m, k); int left = 1, right = n * m; // 最大 9e8 while (left < right) { int mid = left + (right - left) / 2; if (check(m, n, k, mid)) left = mid + 1; else right = mid; } return left; }
boolcheck(int m, int n, int k, int x){ int cnt = 0; for (int i = 1; i <= m; ++i) cnt += min(n, x / i); return cnt < k; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Code language: C boolcheck(int m, int n, int k, int x) { int cnt = 0; for (int i = 1; i <= m; ++i) cnt += n < x / i ? n : x / i; return cnt < k; }
intfindKthNumber(int m, int n, int k){ if (m > n) return findKthNumber(n, m, k); int left = 1, right = n * m; // 最大 9e8 while (left < right) { int mid = left + (right - left) / 2; if (check(m, n, k, mid)) left = mid + 1; else right = mid; } return left; }