Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя suhailrashidsalma

Автор suhailrashidsalma, история, 4 года назад, По-английски

How to use binary search in this question?

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

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 года назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

thanks

  • »
    »
    4 года назад, # ^ |
    Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится
    $$$ 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 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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.