mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
25 lines
420 B
Go
25 lines
420 B
Go
package leetcode
|
|
|
|
import "math"
|
|
|
|
func findKthNumber(m int, n int, k int) int {
|
|
low, high := 1, m*n
|
|
for low < high {
|
|
mid := low + (high-low)>>1
|
|
if counterKthNum(m, n, mid) >= k {
|
|
high = mid
|
|
} else {
|
|
low = mid + 1
|
|
}
|
|
}
|
|
return low
|
|
}
|
|
|
|
func counterKthNum(m, n, mid int) int {
|
|
count := 0
|
|
for i := 1; i <= m; i++ {
|
|
count += int(math.Min(math.Floor(float64(mid)/float64(i)), float64(n)))
|
|
}
|
|
return count
|
|
}
|