← Home

470. Implement Rand10() Using Rand7()

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
470

The trick here is to name the state correctly, then let the implementation follow. For 470. Implement Rand10() Using Rand7(), the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: straightforward implementation.

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

  • Approach 1: Rejection Sampling
  • time: O(1) average, O(infinite) for worst case, space: O(1)
  • The rand7() API is already defined for you.
  • int rand7();

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are rand7, rand10.

Guide

Why?

The solution works because it narrows the problem until every update has a clear reason to exist.

  • 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(1) average, O(infinite) for worst case, 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//Approach 1: Rejection Sampling
02//Runtime: 68 ms, faster than 69.59% of C++ online submissions for Implement Rand10() Using Rand7().
03//Memory Usage: 7.2 MB, less than 100.00% of C++ online submissions for Implement Rand10() Using Rand7().
04//time: O(1) average, O(infinite) for worst case, space: O(1)
05// The rand7() API is already defined for you.
06// int rand7();
07// @return a random integer in the range 1 to 7
08
09class Solution {
10public:
11    int rand10() {
12        int row, col, idx;
13        do{
14            //we can generate 7*7=49 numbers by calling rand7() twice
15            row = rand7();
16            col = rand7();
17            //this is the index on an 2-D table
18            idx = (row - 1)*7 + col;
19        }while(idx > 40);
20        //idx's range : [1-40]
21        //idx-1: [0-39]
22        //(idx-1)%10 : [0-9]
23        //(idx-1)%10+1 : [1-10]
24        return (idx-1) % 10+ 1;
25    }
26};
27
28//Approach 2: Rejection Sampling, Utilizing out-of-range samples(speed up)
29//Runtime: 68 ms, faster than 69.59% of C++ online submissions for Implement Rand10() Using Rand7().
30//Memory Usage: 7.3 MB, less than 100.00% of C++ online submissions for Implement Rand10() Using Rand7().
31// The rand7() API is already defined for you.
32//time: O(1) average, O(infinite) for worst case, space: O(1)
33
34// int rand7();
35// @return a random integer in the range 1 to 7
36
37class Solution {
38public:
39    int rand10() {
40        int row, col, idx;
41        while(true){
42            row = rand7();
43            col = rand7();
44            idx = (row - 1)*7 + col;
45            if(idx <= 40){
46                return (idx-1) % 10+ 1;
47            }
48            
49            row = idx-40; //1 to 9
50            col = rand7();
51            idx = (row - 1)*7 + col; //1 to 63
52            if(idx <= 60){
53                return (idx-1) % 10+ 1;
54            }
55            
56            row = idx-60; //1 to 3
57            col = rand7();
58            idx = (row - 1)*7 + col; //1 to 21
59            if(idx <= 20){
60                return (idx-1) % 10+ 1;
61            }
62        }
63    }
64};

Cost

Complexity

Time
O(1) average, O(infinite) for worst case, 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.