Search code examples
javascriptalgorithmtypescriptmathlogarithm

Math Algorithm Challenge


Why does this solution work? most people are doing loops to solve this and i did recursion, but this is easy as hell, for the computer, i am so confused why does this work?

Description: A child is playing with a ball on the nth floor of a tall building. The height of this floor, h, is known.

He drops the ball out of the window. The ball bounces (for example), to two-thirds of its height (a bounce of 0.66).

His mother looks out of a window 1.5 meters from the ground.

How many times will the mother see the ball pass in front of her window (including when it's falling and bouncing?

Three conditions must be met for a valid experiment: Float parameter "h" in meters must be greater than 0 Float parameter "bounce" must be greater than 0 and less than 1 Float parameter "window" must be less than h. If all three conditions above are fulfilled, return a positive integer, otherwise return -1.

Note: The ball can only be seen if the height of the rebounding ball is stricty greater than the window parameter.

export class G964 {
    public static bouncingBall(h: number, bounce: number, window: number): any {
        if (h <= 0 || bounce >= 1 || bounce <= 0 || window >= h) {
          return -1
        }

        return 1 + 2 * (Math.ceil(Math.log(window / h) / Math.log(bounce)) - 1)
  }
}

Solution

  • This really is more a math problem, but...

    With each bounce your ball bounces a fraction of the bounce before. So to make it easy imagine the bounce parameter is 0.5. Each bounce is half the height of the bounce before. If your window is at 1.0 the bounces will be:

    1/2, 1/4, 1/8, 1/16...
    

    Another way to say this is the height of the bounce will be bounce_rate ** n where n is the number of bounces. The third bounce is 0.5 ** 3 or 1/8.

    Now if you are given a height and you asked which bounce that height corresponds to, you will need to solve the equation height = bounce_rate ** n for n. Solving for n requires a log — in this case n = log(base bounce_rate)height. log base bounce_rate is inconvenient, but you can rewrite this as log(height)/log(bounce_rate). In concrete terms to figure which bounce gets to 1/16 the window height, you just calculate log(1/16)/log(0.5) and get 4.

    So you should be able to start see how this resembles your equation. But since our equation is using height as a fraction of window height, we need to turn that height into something relative to the mothers window. So instead of figuring out when the ball will bounce to something like 1/16 the height of the drop window we want to know when the ball reaches a different ratio — that of the drop window to the mother's — window/h — if the mother's window as at 5 and the ball is dropped from 20, that would be calculating 1/4 which as we previously showed would be log(1/4)/log(bounce_rate) or log(window/h)/log(bounce)

    That's the meat of it. The rest of the equation is dealing with the fact that window is not necessarily an exact solution to log equation, so we round with Math.ceil and the fact that the ball appears in front off the window once on the way down, but twice each time it bounces past window height.