Please read the new rule regarding the restriction on the use of AI tools. ×

suhailrashidsalma's blog

By suhailrashidsalma, history, 4 years ago, In English

How to use binary search in this question?

  • Vote: I like it
  • +14
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Find some $$$f(h)$$$ that gives the number of cards required for a given height $$$h$$$, prove that it's monotonic (that is, if $$$h_1 < h_2$$$, then $$$f(h_1) \leq f(h_2)$$$), and binary search on the lowest $$$h_0$$$ such that $$$f(h_0)$$$ is less than or equal to the cards that you have, then repeat while you can still make towers.

»
4 years ago, # |
  Vote: I like it +6 Vote: I do not like it

thanks

  • »
    »
    4 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it
    $$$ f(h) = {h(3h + 1) \over 2} $$$

    galen_colin Could you please help me in deriving time complexity for given $$${{N}}$$$
    Does it converge to $$${{O(\log(\sqrt N))}}$$$ in the binary search approach ?

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Let's see. $$$f(h + 1) - f(h) = \frac{(h + 1)(3h + 4)}{2} - \frac{h(3h + 1)}{2} = \frac{3h^2 + 7h + 4}{2} - \frac{3h^2 + h}{2} = 4h + 1$$$, which is an upper bound on the remaining number of cards. Since $$$h \approx \sqrt{n}$$$, each iteration the number of cards will become around a square root of itself. So you get $$$\log{n^{\frac{1}{2}}} + \log{n^{\frac{1}{4}}} + \dots = \frac{\log{n}}{2} + \frac{\log{n}}{4} + \dots... = \log{n}$$$, approximately. Maybe the factor of $$$4$$$ plays a role, but probably not too much of one.