zoukankan      html  css  js  c++  java
  • Arrange seat of a bench for people

    Given a bench with n seats and few people sitting, tell the seat number each time when a new person goes to sit on the bench such that his distance from others is maximum?

    G jia

    A few clarifying questions that i would ask:
    1. What if the bench is emtpy. Can i return any seat number?
    2. What if bench is full. Should i return -1.
    3. How big can n be?
    4. How many queries are we making?

    Algo:
    1. Traverse the bench once (saved as array or vector) and populate a max heap based on distance between two occupied seats. For each node in heap have distance, start seat no and end seat no.
    2. When a query is made return the seat no as (end seat no - start seat no)/2 for the root node in heap.
    3. Replace the root node with one node formed by (end seat no - start seat no)/2 and end seat no and call heapIfY() on heap.
    4. insert the seconds node formed by start node no and (end seat no - start seat no)/2 in the heap.

    Repeat process from 2 to 4 for each query.

    Complexity:
    Forming heap: O(n)
    For each query: O(log(n))

  • 相关阅读:
    JavaScript简单的日历
    CSS动画实现滚动播放
    视频弹窗自动播放 关闭停止
    简单通过js实现轮播图
    switch case的应用
    显示、隐藏
    .container的应用
    用CSS对HTML进行初始化设置
    CSS Id 和 Class选择器
    给文字添加阴影效果
  • 原文地址:https://www.cnblogs.com/lightwindy/p/9711296.html
Copyright © 2011-2022 走看看