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))

  • 相关阅读:
    端口
    log4j常用配置以及日志文件保存位置
    jbpm node signal
    JBPM3.2 TABLE
    JBPM TaskInstance 对象创建过程
    【转】链接脚本
    快速平方根倒数
    GPS开发之知识储备(NMEA0183)
    HEX文件格式和其校验算法
    NRF51822之IIC(MEMS_LIS2DH12)
  • 原文地址:https://www.cnblogs.com/lightwindy/p/9711296.html
Copyright © 2011-2022 走看看