← Home

668. Kth Smallest Number in Multiplication Table

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
binary searchC++Markdown
668

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 668. Kth Smallest Number in Multiplication Table, the solution in this repository is mainly a binary search solution.

Guide

What?

The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: binary search.

The notes already sitting in the source point us in the right direction:

  • time: O(m*log(mn)), space: O(1)

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findKthNumber.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(m*log(mn)), space: O(1)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 28 ms, faster than 50.39% of C++ online submissions for Kth Smallest Number in Multiplication Table.
02//Memory Usage: 5.9 MB, less than 100.00% of C++ online submissions for Kth Smallest Number in Multiplication Table.
03//time: O(m*log(mn)), space: O(1)
04class Solution {
05public:
06    int findKthNumber(int m, int n, int k) {
07        int lo = 1, hi = m*n;
08        int mid;
09        
10        while(lo <= hi){
11            mid = lo + (hi-lo)/2;
12            
13            //get the count of numbers <= mid
14            int count = 0;
15            for(int i = 1; i <= m; i++){
16                count += min(n, mid/i);
17            }
18            
19            // cout << lo << ", " << mid << ", " << hi << ": " << count << endl;
20            
21            if(k > count){
22                lo = mid+1;
23            }else if(k <= count){
24                hi = mid-1;
25            }
26        }
27        
28        return lo;
29    }
30};

Cost

Complexity

Time
O(m*log(mn)), space: O(1)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.