O(nlogn) Algorithm - Find three evenly spaced ones within binary string

前端 未结 30 3006
刺人心
刺人心 2020-11-28 00:07

I had this question on an Algorithms test yesterday, and I can\'t figure out the answer. It is driving me absolutely crazy, because it was worth about 40 points. I figure

30条回答
  •  伪装坚强ぢ
    2020-11-28 00:49

    Assumption:

    Just wrong, talking about log(n) number of upper limit of ones

    EDIT:

    Now I found that using Cantor numbers (if correct), density on set is (2/3)^Log_3(n) (what a weird function) and I agree, log(n)/n density is to strong.

    If this is upper limit, there is algorhitm who solves this problem in at least O(n*(3/2)^(log(n)/log(3))) time complexity and O((3/2)^(log(n)/log(3))) space complexity. (check Justice's answer for algorhitm)

    This is still by far better than O(n^2)

    This function ((3/2)^(log(n)/log(3))) really looks like n*log(n) on first sight.

    How did I get this formula?

    Applaying Cantors number on string.
    Supose that length of string is 3^p == n
    At each step in generation of Cantor string you keep 2/3 of prevous number of ones. Apply this p times.

    That mean (n * ((2/3)^p)) -> (((3^p)) * ((2/3)^p)) remaining ones and after simplification 2^p. This mean 2^p ones in 3^p string -> (3/2)^p ones . Substitute p=log(n)/log(3) and get
    ((3/2)^(log(n)/log(3)))

提交回复
热议问题