-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kata: 54cb771c9b30e8b5250011d4
- Loading branch information
Showing
5 changed files
with
76 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/scala/sierikov/codewars/kyu3/FabergeEasterEgg.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package sierikov.codewars.kyu3 | ||
|
||
import java.math.BigInteger | ||
import java.math.BigInteger.{ONE, ZERO} | ||
|
||
object FabergeEasterEgg { | ||
|
||
/** | ||
* Use binomial coefficient to calculate the number of ways to distribute the tries among the eggs. | ||
* The number of ways to distribute the tries among the eggs is the sum of the binomial coefficients. | ||
* Applied dynamic programming to pass the performance tests. | ||
* @note Using BigIntegers due kata requirements. | ||
* @note Using cumulative sum of coefficient to avoid recalculating the same values. | ||
* @see [[https://en.wikipedia.org/wiki/Binomial_coefficient]] | ||
*/ | ||
def height(eggs: BigInteger, tries: BigInteger): BigInteger = { | ||
if (eggs == ZERO || tries == ZERO) return ZERO | ||
|
||
/** | ||
* Having more eggs than tries doesn't provide any additional advantage. | ||
* The limiting factor in determining the maximum height is the number of tries, not the number of eggs. | ||
*/ | ||
val numberOfEggs = if (eggs.compareTo(tries) > 0) BigInt(tries) else BigInt(eggs) | ||
|
||
var currentEgg = ONE | ||
var remainingFloors = ONE | ||
var result = ZERO | ||
|
||
while (currentEgg.compareTo(numberOfEggs) <= 0) { | ||
val remainTries = tries.add(ONE).subtract(currentEgg) | ||
val newFloors = | ||
remainingFloors.multiply(remainTries).divide(currentEgg) // evenly distribute the tries among the eggs | ||
|
||
remainingFloors = newFloors | ||
currentEgg = currentEgg.add(ONE) | ||
result = result.add(newFloors) | ||
} | ||
|
||
result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/test/scala/sierikov/codewars/kyu3/FabergeEasterEggSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package sierikov.codewars.kyu3 | ||
|
||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers.shouldBe | ||
|
||
import java.math.BigInteger | ||
|
||
class FabergeEasterEggSpec extends AnyFlatSpec { | ||
|
||
import FabergeEasterEgg._ | ||
|
||
private val testCases = List( | ||
(0, 0, "0"), | ||
(1, 51, "51"), | ||
(2, 1, "1"), | ||
(4, 17, "3213"), | ||
(16, 19, "524096"), | ||
(6015, 5, "31"), | ||
(23, 19, "524287"), | ||
// stress test | ||
(13, 550, "60113767426276772744951355") | ||
).map { case (n, m, expected) => | ||
(BigInteger.valueOf(n), BigInteger.valueOf(m), BigInteger(expected)) | ||
} | ||
|
||
testCases.foreach { case (n, m, expected) => | ||
it should s"return $expected for n = $n, m = $m" in { | ||
height(n, m) shouldBe expected | ||
} | ||
} | ||
|
||
} |