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. eclipse使用内置tomcat和使用外部tomcat的设置

    近期由于项目中jsp发请求要訪问项目以外的文件.直接訪问写成"c:\xxx\xxx.mp4"来訪问是没有权限的.不能完毕现有要求.经查询后发现能够在tomcat中配置虚拟文件夹将本 ...

  2. Revit API创建一个拷贝房间内对象布局命令

    本课程演示创建一个拷贝房间内对象布局命令,完整演示步骤和代码.这个命令把选中房间内的对象复制到其它选中的一个或多个房间中,而且保持与源房间一致的相对位置.通过本讲座使听众知道创建一个二次开发程序很简单 ...

  3. C++中的vector&find_if

     <STL應用> vector & find_if 看到有人問有個名為C的struct如下 code: struct C { int v1; int v2; }; 應用在vecto ...

  4. sqlite3 插入数据的时候,返回SQLITE_CONSTRAINT

    sqlite3 插入数据的时候.返回SQLITE_CONSTRAINT 原因是:数据库的表的名字是纯数字. 大改这个原因太诡异了.创建的时候能够创建成功. 插入数据的时候就失败,由于表名是纯数字. 附 ...

  5. android 工具类 DateUtil

    提取了一些在开发过程中可能会用到的日期相关的函数作为工具类.供大家參考: /** * 日期操作工具类. * * @author shimiso */ public class DateUtil { p ...

  6. 手机端可以和PC端同时在线-java QRCode 实现网站扫码登录(即支持同帐号多设备同时登录)

    微信扫码测试地址:: http://sms.reyo.cn 用户名:aa 密码:123456 扫码登录实现方式很多,比如ajax轮询,http长连接(comet...),websocket,event ...

  7. Android之循环显示图像的Android Gallery组件

    转自:http://www.blogjava.net/nokiaguy/archive/2010/08/23/329721.html Gallery组件主要用于横向显示图像列表,不过按常规做法.Gal ...

  8. ExtJS 4.2 教程-01:Hello ExtJS

    转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-1-hello-extjs, 本文还发布在了ExtJS教程网站起飞网上面,如果转载请保留本段声明,谢谢合作 ...

  9. mongodb centos7上的安装

    1,下载安装包 下载mongoDB的安装文件地址:https://www.mongodb.org/downloads#production 选择Linux 64-bit legacy 版本,下载到目标 ...

  10. 最课程启示录:L风的李同学

    李同学是一个特殊的存在. 他永远是学员群里话最多的一个男同学.注意,这里加了一个定语“男”,这当然意味着不久的将来,我们的学员启示录将会出现一个话多的女同学. 我并不是第一天知道他话多.据说,他在来最 ...