Math problem, Java AC code with brief explanations, 11/09/2016


  • 4

    This is a Math problem.
    The table below shows how it works, n denotes the pig, and s is associated with each status of the pig.
    for each pig, there is t+1 status, which is death in 1st, 2nd, 3rd, 4th time slot and live eventually.
    Therefore the question is asking for how many pigs can denote 1000 in 5-nary number system.
    =======Table=======
    n1 n2 n3 n4 n5
    s1 1 0 0 1 0
    s2 0 0 0 0 1
    s3 0 1 0 0 0
    s4 0 0 0 0 0
    s5 0 0 1 0 0

    In this case, the number is 13012 in 5-nary number system.

    • Here is my Java AC code
    public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
            int status = minutesToTest/minutesToDie + 1;
            int num_of_pig = 0;
            while(Math.pow(status, num_of_pig) < buckets) {num_of_pig++;}
            return num_of_pig;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.