From 319a2cc242bc18aa19c8ea3135f5914434cfe718 Mon Sep 17 00:00:00 2001 From: YDZ Date: Thu, 7 Nov 2019 12:06:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20668?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Smallest Number in Multiplication Table.go | 24 +++++++ ...est Number in Multiplication Table_test.go | 64 +++++++++++++++++++ .../README.md | 52 +++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 Algorithms/0668. Kth Smallest Number in Multiplication Table/668. Kth Smallest Number in Multiplication Table.go create mode 100644 Algorithms/0668. Kth Smallest Number in Multiplication Table/668. Kth Smallest Number in Multiplication Table_test.go create mode 100755 Algorithms/0668. Kth Smallest Number in Multiplication Table/README.md diff --git a/Algorithms/0668. Kth Smallest Number in Multiplication Table/668. Kth Smallest Number in Multiplication Table.go b/Algorithms/0668. Kth Smallest Number in Multiplication Table/668. Kth Smallest Number in Multiplication Table.go new file mode 100644 index 00000000..c6c63d70 --- /dev/null +++ b/Algorithms/0668. Kth Smallest Number in Multiplication Table/668. Kth Smallest Number in Multiplication Table.go @@ -0,0 +1,24 @@ +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 +} diff --git a/Algorithms/0668. Kth Smallest Number in Multiplication Table/668. Kth Smallest Number in Multiplication Table_test.go b/Algorithms/0668. Kth Smallest Number in Multiplication Table/668. Kth Smallest Number in Multiplication Table_test.go new file mode 100644 index 00000000..8285b611 --- /dev/null +++ b/Algorithms/0668. Kth Smallest Number in Multiplication Table/668. Kth Smallest Number in Multiplication Table_test.go @@ -0,0 +1,64 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question668 struct { + para668 + ans668 +} + +// para 是参数 +// one 代表第一个参数 +type para668 struct { + m int + n int + k int +} + +// ans 是答案 +// one 代表第一个答案 +type ans668 struct { + one int +} + +func Test_Problem668(t *testing.T) { + + qs := []question668{ + + question668{ + para668{3, 3, 5}, + ans668{3}, + }, + + question668{ + para668{2, 3, 6}, + ans668{6}, + }, + + question668{ + para668{1, 3, 2}, + ans668{2}, + }, + + question668{ + para668{42, 34, 401}, + ans668{126}, + }, + + question668{ + para668{7341, 13535, 12330027}, + ans668{2673783}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 668------------------------\n") + + for _, q := range qs { + _, p := q.ans668, q.para668 + fmt.Printf("【input】:%v 【output】:%v\n", p, findKthNumber(p.m, p.n, p.k)) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0668. Kth Smallest Number in Multiplication Table/README.md b/Algorithms/0668. Kth Smallest Number in Multiplication Table/README.md new file mode 100755 index 00000000..2d1f3526 --- /dev/null +++ b/Algorithms/0668. Kth Smallest Number in Multiplication Table/README.md @@ -0,0 +1,52 @@ +# [668. Kth Smallest Number in Multiplication Table](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/) + + +## 题目: + +Nearly every one have used the [Multiplication Table](https://en.wikipedia.org/wiki/Multiplication_table). But could you find out the `k-th` smallest number quickly from the multiplication table? + +Given the height `m` and the length `n` of a `m * n` Multiplication Table, and a positive integer `k`, you need to return the `k-th` smallest number in this table. + +**Example 1:** + + Input: m = 3, n = 3, k = 5 + Output: + Explanation: + The Multiplication Table: + 1 2 3 + 2 4 6 + 3 6 9 + + The 5-th smallest number is 3 (1, 2, 2, 3, 3). + +**Example 2:** + + Input: m = 2, n = 3, k = 6 + Output: + Explanation: + The Multiplication Table: + 1 2 3 + 2 4 6 + + The 6-th smallest number is 6 (1, 2, 2, 3, 4, 6). + +**Note:** + +1. The `m` and `n` will be in the range [1, 30000]. +2. The `k` will be in the range [1, m * n] + + +## 题目大意 + +几乎每一个人都用乘法表。但是你能在乘法表中快速找到第 k 小的数字吗?给定高度 m 、宽度 n 的一张 m * n 的乘法表,以及正整数 k,你需要返回表中第 k 小的数字。 + + +注意: + +- m 和 n 的范围在 [1, 30000] 之间。 +- k 的范围在 [1, m * n] 之间。 + +## 解题思路 + +- 给出 3 个数字,m,n,k。m 和 n 分别代表乘法口诀表的行和列。要求在这个乘法口诀表中找第 k 小的数字。 +- 这一题是第 378 题变种题。利用二分搜索,在 `[1,m*n]` 的区间内搜索第 `k` 小的数。每次二分统计 `≤ mid` 数字的个数。由于是在两数乘法构成的矩阵中计数,知道乘数,被乘数也就知道了,所以计数只需要一层循环。整体代码和第 378 题完全一致,只是计数的部分不同罢了。可以对比第 378 题一起练习。