zoukankan      html  css  js  c++  java
  • Given life time of different animals. Find period when maximum number of animals lived

    Q:

    Given life time of different animals. Find period when maximum number of animals lived.

    ex [5, 11], [6, 18], [2, 5],[3,12] etc. year in which max no animals exists.

    A:

    Input intervals (or lifetimes): [5, 11], [6, 18], [2, 5], [3,12]

    1. Put the end and start times of the intervals in one array. Sort it!. Always put the start time before end time in case they are equal. Maintain flags to identify a start/end interval. For above input I'll do something like:
    2S, 3S, 5S, 5E, 6S, 11E, 12E, 18E

    2. Now scan the array from left to right keeping track of how many intervals we are in. (This would be equal to total numbers of start intervals - total number of end intervals encountered so far). For above input I'll get something like:
    1, 2, 3, 2, 3, 2, 1, 0

    3. Now pick the maxima points from step 2. All the maxima points will be Start intervals and the point next to a maxima point will always be an end interval (which will be the end of the maxima start interval). So we'll get:
    [5S,5E] and [6S,11E].

    Hence the result is [5,5], [6,11]

  • 相关阅读:
    V8 下的垃圾回收机制
    数据库索引原理
    多线程的实现方法
    网元的概念
    Oracle 数据库实现数据合并:merge
    Linux账号管理
    Linux 进程管理 ps、top、pstree命令
    linux OS与SQL修改时区,系统时间
    数据库的几种模式
    linux上限值网速、限值带宽
  • 原文地址:https://www.cnblogs.com/yayagamer/p/3129529.html
Copyright © 2011-2022 走看看