JavaScript Solution with Detail Explanation
Solution Explanation\nLet\'s simulate with a few examples.\n\n##### Example 1\nLet\'s say we have 3 rounds to test and need to test 4 buckets. To find out the p
#### Solution Explanation\nLet\'s simulate with a few examples.\n\n##### Example 1\nLet\'s say we have 3 rounds to test and need to test 4 buckets. To find out the poisonous bucket we need 1 pig. \n\n**Simulation**\n```\nRound 1: Pig 1 drink 1st bucket. \n\t\tIf it dies then, Bucket 1 is the poisonous bucket. \nRound 2: If Pig 1 survive Round 1 lets test 2nd Bucket\n\t\tIf it dies then, Bucket 2 is the poisonous bucket. \nRound 3: If Pig 1 survive Round 2 lets test 3rd Bucket\n\t\tIf it dies then, Bucket 3 is the poisonous bucket. \n\t\t\nIf the pig survive round 3 then `Bucket 4` is the poisonous\n```\nWe do not need to test anymore. Eighter the pig die or live we can say after `Round 3` which bucket is poisonous. \n\n**Conclusion**\nWe can test `n + 1` buckets with 1 pig. where n is the number of rounds we can test.\n\n##### Example 2\nLet\'s say we have 2 rounds to test and need to test 9 buckets. To find out the poisonous bucket we need a minimum of 2 pigs. \n\nLet\u2019s rearrange the buckets in `Row x Col` fashion.\n```\n  | 0 | 1 | 2 | ---> Col\n--+---+---+---+\n0 | 1 | 2 | 3 |\n--+---+---+---+\n1 | 4 | 5 | 6 |\n--+---+---+---+\n2 | 7 | 8 | 9 |\n--+---+---+---+\n```\n\nHere Row and Col indicates Round and Pigs `(0 indexed)`. \n\n**Simulation**\n\n```\nRound 1: Pig 1 drink Bucket 1, 2, 3. And Pig 2 drink Bucket 1 4 7\n\t\tIf both Pigs died then It\u2019s Bucket 1 (common bucket). If Pig 1 die and Pig 2 survive\n\t\tThen it\u2019s Bucket 2 or 3 (if it\u2019s 1 then Pig 2 also died). \nRound 2: Now Pig 2 drink Bucket 2.\n\t\tIf it dies then Bucket 2 poisonous or if survive Bucket 3 is poisonous.\nSame Goes for Pig 1. If Pig 2 die in Round 1 and Pig 1 Survive. Then it\u2019s Bucket 4 or 7 (if it\u2019s 1 then Pig 1 also died). \n\nRound 2: Now Pig 1 drink Bucket 4.\n\t\tIf it dies then Bucket 4 poisonous or if survive Bucket 7 is poisonous.\n```\n\nSo far we describe If Both or one of the pig died in `Round 1`. \nLet\'s Now simulate if Both pigs survive `Round 1`.\n\n```\nIf both pigs survive `Round 1` then It\'s definitely not 1 2 3 4 7 Bucket. So answer lies within Bucket 5 6 8 9\n\nRound 2: Pig 1 drink Bucket 5 6. And Pig 2 drink Bucket 5 8\n\t\tIf both Pigs died then It\u2019s Bucket 5 (common bucket). If Pig 1 die and Pig 2 survive\n\t\tThen it\u2019s Bucket 6 (if it\u2019s 5 then Pig 2 also died). \n\t\t\nSame goes for Pig 1. If Pig 2 die in Round 2 and Pig 1 Survive. Then it\u2019s Bucket 8 (if it\u2019s 5 then Pig 1 also died). \n\nIf both survive Round 2 then, Bucket 9 is poisonous.\n```\n\n**Conclusion**\nWe can test `(n + 1) ^ 2` buckets with 2 pig. where n is the number of rounds we can test.\n\nBy simulating these 2 examples we can say `if there is n round then we can test (n + 1) ^ p buckets`  \n\nWe know the number of buckets and rounds so we can easily solve the above equation to find the minimum number of pigs.\n\n#### Big question WHY n + 1?\n To be honest I can\'t mathematically prove why we need n + 1. But I can say if there are 2 buckets and 1 round available we just had to drink 1 bucket and then we can easily decide which bucket is poisonous after observing the pig survive Round 1 or not.\n\n   _***If anyone has a mathematical explanation please me know. I love to learn that._\n   \n ---\n \n #### Big Thanks To\n ---\n1. https://leetcode.com/sanzee/\n2. https://leetcode.com/problems/poor-pigs/discuss/935581/C%2B%2BPythonPicture-Straightforward-solution-with-N-dimension-puzzle-cube-scan\n3. https://www.cnblogs.com/grandyang/p/7664088.html\n ---\n \n ##### Amortized Analysis\n ---\n Time Complexity: O(1)\n Space Complexity: O(1)\n \nRuntime: 76 ms\nMemory Usage: 38.2 MB\n\n---\n##### Code\n---\n```\n/**\n * @param {number} buckets\n * @param {number} minutesToDie\n * @param {number} minutesToTest\n * @return {number}\n */\nvar poorPigs = function(buckets, minutesToDie, minutesToTest) {\n  let answer = 1;\n  let n = minutesToTest / minutesToDie >> 0;\n  n += 1;\n  \n  // calculation with loop\n  // while(n ** answer <= buckets) {\n  //   answer++;\n  // }\n  \n  return Math.ceil(Math.log(buckets) / Math.log(n));\n};\n\n```\n\n\n
['JavaScript']