Eulampy has a collection of some almost identical Fabergè eggs. One day his friend Tempter said to him:
- Do you see that skyscraper? And can you tell me a maximal floor that if you drop your egg from will not crack it?
- No, - said Eulampy.
- But if you give me N eggs, - says Tempter - I'll tell you an answer.
- Deal - said Eulampy. But I have one requirement before we start this: if I will see more than M falls of egg, my heart will be crushed instead of egg. So you have only M tries to throw eggs. Would you tell me an exact floor with this limitation?
Your task is to help Tempter - write a function
height :: Integer -> Integer -> Integer
height n m = -- see text
that takes 2 arguments - the number of eggs n
and the number of trys m
- you should calculate maximum skyscraper height (in floors), in which it is guaranteed to find an exactly maximal floor from which that an egg won't crack it.
Which means,
You can throw an egg from a specific floor every try
Every egg has the same, certain durability - if they're thrown from a certain floor or below, they won't crack. Otherwise, they crack.
You have
n
eggs andm
tries
What is the maximum height, such that you can always determine which floor the target floor is when the target floor can be any floor between 1 to this maximum height?
Examples
height 0 14 = 0
height 2 0 = 0
height 2 14 = 105
height 7 20 = 137979
Data range
n <= 20000
m <= 20000
This challenge comes from Ivana on CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (5)
I don't understand, what is the difference between a try and an egg?
Seems to me like it's
I don't understand how the example results were produced.
You can't make mayonnaise out of a try. But you can try to make mayonnaise out of an egg (don't forget the oil and the mustard though).
Jesus Christ
Maybe because 🅱️inary search is 🅱️rogramming? 🤷
Edit: 🅱️ 🅱️ 🅱️ 🅱️
I was once asked a similar question in an interview. I didn't know it back then and I can't remember it now..