Algorithm interview from Google

前端 未结 8 733
囚心锁ツ
囚心锁ツ 2021-01-30 11:55

I am a long time lurker, and just had an interview with Google where they asked me this question:

Various artists want to perform at the Royal Albert Hall and you are re

8条回答
  •  你的背包
    2021-01-30 12:19

    Store the scheduled concerts in a binary search tree and find a feasible solution by doing a binary search.

    Something like this:

    FindDateAfter(tree, x):
      n = tree.root
      if n.date < x 
        n = FindDateAfter(n.right, x)
      else if n.date > x and n.left.date < x
        return n
      return FindDateAfter(n.left, x)
    
    FindGoodDay(tree, x):
      n = FindDateAfter(tree, x)
      while (n.date + 10 < n.right.date)
        n = FindDateAfter(n, n.date + 5)
      return n.date + 5
    

提交回复
热议问题