Give you set of meetings start time and end time, count how many meeting rooms needed.

For example:

[[, ], [, ], [, ], [, ]] // needs 3 meeting rooms

We can use a memo table to store the at which time how many rooms needed. It is sort of like Knapsack problem. Check out this post for similar question.

Code:

function* generateAry(start, end) {
let i = start;
while (i <= end) {
yield i;
i++;
}
} // O(N*K): K: number of hours, N: number of room
// S(K)
function availableRoom(arys) {const { min, max } = arys.reduce(
(acc, ary) => {
const [start, end] = ary;
let { min, max } = acc;
if (start < min) {
min = start;
}
if (end > max) {
max = end;
}
return { min, max };
},
{ min: Infinity, max: }
);
const hours = Array.from(generateAry(min, max));
const memo = hours.map(x => ); function helper(arys, hours, memo) {
let countRooms = ; for (let row in arys) {
for (let col in hours) {
let prevCount = memo[col];
let [min, max] = arys[row];
// if the min > hour, set previous row same col value, the same as max < hour
if (min > hours[col] || max < hours[col]) {
memo[col] = prevCount;
continue;
} if (min <= hours[col] && max >= hours[col]) {
memo[col] = prevCount + ;
} countRooms = Math.max(countRooms, memo[col]);
}
}
console.log(memo); // [1, 3, 3, 2, 2, 2, 2, 1]
return countRooms;
} return helper(arys, hours, memo);
} console.log(availableRoom([[, ], [, ], [, ], [, ]])); // 3

[Algorithm] How many meeting rooms needed?的更多相关文章

  1. [LeetCode] Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  2. [LeetCode] Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  3. LeetCode Meeting Rooms II

    原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...

  4. LeetCode Meeting Rooms

    原题链接在这里:https://leetcode.com/problems/meeting-rooms/ Given an array of meeting time intervals consis ...

  5. [LeetCode] Meeting Rooms I & II

    Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s ...

  6. 252. Meeting Rooms 区间会议室

    [抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...

  7. [LeetCode] 253. Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  8. [LeetCode] 252. Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  9. [LeetCode] 253. Meeting Rooms II 会议室 II

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

随机推荐

  1. LPC43xx Asymmetric Dual Core : Cortex-M0 and Cortex-M4

  2. Android Studio 怎样打JAR包

    Android Studio 怎样打JAR包 在eclipse中我们知道怎样将一个项目导出为jar包,供其他项目使用. 在AS中能够通过改动gradle才处理. 我们新建一个项目MakeJar,在项目 ...

  3. delphi dxRibbon中 F10快捷键不好用的原因

    最近在项目中使用ribbon  ,用F10做快捷键,但是不好用, 不好用的原因是dxBarManager1 中的有个选项UseF10ForMenu, 把这项关闭就可以了

  4. Win10年度更新开发必备:VS2015 Update 1正式版下载汇总

    微软在12月1日发布了Visual Studio 2015 Update 1 .在MSDN中微软也提供下载,而且MSDN的Visual Studio 2015 Update 1与官方免费下载的文件是一 ...

  5. CLR是如何被加载并工作的

    当运行Windows应用程序的时候,CLR总是默默地为服务着.CLR到底是如何被加载并运行呢? 首先,Microsoft专门为CLR定义了一个标准的COM接口. 安装某个版本的.NET Framewo ...

  6. Android:活动的启动模式

    启动模式一共有四种,分别是 standard .singleTop . singleTask 和 singleInstance , 可 以 在 AndroidManifest.xml 中 通 过 给 ...

  7. Android:intent的基础

    只有一个活动的应用也太简单了吧?没错,你的追求应该更高一点.不管你想创建多少 个活动,方法都和上一节中介绍的是一样的.唯一的问题在于,你在启动器中点击应用的图 标只会进入到该应用的主活动,那么怎样才能 ...

  8. lastlog

    [root@li739-39 ~]# lastlogUsername Port From Latestroot pts/1 183.15.253.245 Thu Oct 29 05:57:29 +00 ...

  9. C#编程(五十六)----------可观察的集合ObservableCollection

    原文链接: http://blog.csdn.net/shanyongxu/article/details/47080685 可观察的集合 如果需要集合中的元素核实删除或添加的信息,就可以使用Obse ...

  10. .NET:枚举的默认值

    .NET中的值类型默认都会设置为0,枚举也是如此,因此当你定义自己的枚举值类型且显式的指定了枚举值时,别忘记使用0,如果由于某种原因不能使用0,如使用了Flag标记,则别忘记在使用了枚举类型的构造方法 ...