Kth Smallest Number in Multiplication Table - Complete Solution Guide
Kth Smallest Number in Multiplication Table is LeetCode problem 668, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Nearly everyone has used the Multiplication Table . The multiplication table of size m x n is an integer matrix mat where mat[i][j] == i * j ( 1-indexed ). Given three integers m , n , and k , return the k th smallest element in the m x n multiplication table . Example 1: Input: m = 3, n = 3, k = 5 Output: 3 Explanation: The 5 th smallest number is 3. Example 2: Input: m = 2, n = 3, k = 6 Output: 6 Explanation: The 6 th smallest number is 6. Constraints: 1 <= m, n <= 3 * 10 4 1 <= k <= m * n
Detailed Explanation
The problem asks us to find the k-th smallest number in an m x n multiplication table. The multiplication table is a matrix where the element at row i and column j is equal to i * j (1-indexed). We are given m, n, and k, and need to return the k-th smallest element.
Solution Approach
The solution uses binary search to find the k-th smallest element. It searches through the possible values from 1 to m*n. For each mid-value during the binary search, the `count_le` function counts the number of elements in the multiplication table that are less than or equal to the `mid` value. If the count is greater than or equal to k, then we know the k-th smallest element is less than or equal to mid, so we narrow down the search space by setting high = mid. Otherwise, if the count is less than k, we narrow the search space by setting low = mid + 1. Eventually, low and high converge to the k-th smallest element.
Step-by-Step Algorithm
- Step 1: Initialize low to 1 and high to m * n. These represent the range of possible values for the k-th smallest element.
- Step 2: Perform binary search while low < high.
- Step 3: Calculate mid = low + (high - low) // 2. This avoids potential integer overflow issues.
- Step 4: Call the `count_le` function to count the number of elements in the multiplication table that are less than or equal to mid.
- Step 5: If `count_le(mid)` is greater than or equal to k, it means the k-th smallest number is less than or equal to mid, so set high = mid.
- Step 6: Otherwise, if `count_le(mid)` is less than k, it means the k-th smallest number is greater than mid, so set low = mid + 1.
- Step 7: After the while loop finishes, low will be equal to the k-th smallest number. Return low.
Key Insights
- Insight 1: The multiplication table is inherently sorted. However, a complete sort is computationally expensive. We can leverage the sorted nature implicitly using binary search.
- Insight 2: Binary search can be applied on the possible values in the multiplication table. The lower bound is 1, and the upper bound is m * n. For any 'mid' value in the search space, we need to efficiently count how many elements are less than or equal to 'mid' in the multiplication table.
- Insight 3: Counting elements <= mid can be optimized. For each row 'i', the number of elements <= mid is min(n, mid // i). If mid // i is 0, it means there are no elements in the row that are <= mid, and we can break early.
Complexity Analysis
Time Complexity: O(m*log(m*n))
Space Complexity: O(1)
Topics
This problem involves: Math, Binary Search.
Companies
Asked at: DE Shaw.