Implement Rand10() Using Rand7() | LeetCode 470 | C++, Java, Python

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

August LeetCoding Challenge | Problem 28 | Implement Rand10() Using Rand7() | 28 August,
Facebook Coding Interview question,
google coding interview question,
leetcode,
Implement Rand10() Using Rand7(),
Implement Rand10() Using Rand7() c++,
Implement Rand10() Using Rand7() Java,
Implement Rand10() Using Rand7() python,
Implement Rand10() Using Rand7() solution,
470. Implement Rand10() Using Rand7(),

#CodingInterview #LeetCode #AugustCodingChallenge #Google #Amazon #rand10
Рекомендации по теме
Комментарии
Автор

Thanks as always. I guess you can't avoid an indeterminate solution. Makes me feel better about my submission.
A slight optimization would be to use the first roll for V2 if it came up 6 or 7, then run the loop for V1. Would make the code bigger though.

crankyinmv
Автор

Can this theoretically be done in O(1) with a limited number of calls to the RNG?

WASDsweden
Автор

Will this approach be optimal for any Implement RandM() using RandN()

chaitanyayoyo
Автор

Great explanation ! @Knowledge Center How to calculate the probabailities of each number based on the solution?

BULLSEYEII
Автор

1. first take one arr of size 49 (having value - 0)
int arr[49]={0};

2. then make one value - 1
arr[rand7()*rand7()-1]=1;

3. now let's make rand10()

int val1= rand7();
int val2= rand7();
int random_val = ((val1*val2)+arr[v1+v2-1])%10 + 1;

darshanbhimani
Автор

Hello sir! It's me I have said about my problem previously with trees and DP. YOU said practice more recursion. I'm trying but I'm getting stuck frequently and I feel like I'm not coming from fundamentals. If you have some free time, please create a bucket list for practing these topics for me. Thanks for your support 💖

ayyappahemanth
Автор

whats wrong with rand7()+rand7()%4;
can any one explain.

nitishk
Автор

why this ( ( rand7()+rand7() )%10 + 1 ) is not working??

acxd
Автор

Whats wrong with return rand7() + 3 !? Please explain!

SR-wevl
Автор

Im used one line of code in python random.randint(1, 10) and it beats 99% any difference sir

alagargopal
Автор

WHY this way is not working?

class Solution {
public:
int c=0;
int rand10() {
c++;
if(c<8)
{
return rand7();
}
else
{
if(c==11)
{
c=0;
return 10;
}
return c;

}
}
};

jaydave
welcome to shbcf.ru