[Algorithm] How many meeting rooms needed?
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?的更多相关文章
- [LeetCode] Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] Meeting Rooms 会议室
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- LeetCode Meeting Rooms II
原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...
- LeetCode Meeting Rooms
原题链接在这里:https://leetcode.com/problems/meeting-rooms/ Given an array of meeting time intervals consis ...
- [LeetCode] Meeting Rooms I & II
Meeting Rooms Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s ...
- 252. Meeting Rooms 区间会议室
[抄题]: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.. ...
- [LeetCode] 253. Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 252. Meeting Rooms 会议室
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 253. Meeting Rooms II 会议室 II
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
随机推荐
- gitblit.cmd运行自动关闭
前几天运行gitblit.cmd一直正常,今天运行gitblit.cmd,几秒钟后命令行窗口就自动关闭了,导致无法启动gitblit服务器,查看日志如下: 刚开始以为是防火墙问题,在防火墙中添加了程序 ...
- 虫趣:BAD POOL CALLER (par1: 0x20)
[作者:张佩] [原文:http://www.yiiyee.cn/Blog/0x19-1/] 内核在管理内存的时候,为了提高内存使用效率,对于小片内存的申请(小于一个PAGE大小),都是通过内存池来操 ...
- 内核调试神器SystemTap 转摘
http://blog.csdn.net/zhangskd/article/details/25708441 https://sourceware.org/systemtap/wiki/WarStor ...
- C#消息队列(RabbitMQ)零基础从入门到实战演练
一.课程介绍 如果您从工作中之听过但未有接触过消息对队列(MQ),如果你接触过一点关于MQ的知识,如果没有这么的多如果的话......,那么阿笨将通过本次<C#消息队列零基础从入门到实战演练&g ...
- JAVA各种系统架构图及其简介
1.spring架构图 Spring是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为J2EE应用程序开发提供集成的框 ...
- Xamarin.Android,Xamarin.iOS, Linking
Xamarin.Android applications use a linker in order to reduce the size of the application. The linker ...
- 恶意软件正在利用SSLserver窃取用户个人信息!
安全套接层协议(SSL)及安全传输层协议(TLS)旨在提供一个安全.加密的client和server之间的连接网络.为进一步进行身份验证和加密,server必须提供证书,从而直接有效地证明其身份. 使 ...
- mysql between and 遇到日期查询边界问题
最近实现一个按日期范围查询列表,例如输入的是日期 2015-11-01到2015-11-03,想得到1号到3号的数据, 执行 select * from table where create_date ...
- apache路由端口配置
<VirtualHost *:80> ServerName a.com ProxyPreserveHost On ProxyRequests On ProxyPass / http://1 ...
- js混淆加密,通过混淆Js代码让别人(很难)无法还原
js混淆加密,通过混淆Js代码让别人(很难)无法还原 使用js的混淆加密,其目的是为了保护我们的前端代码逻辑,对应一些搞技术吃饭的公司来说,为了防止被竞争对手抓取或使用自己的代码,就会考虑如何加密 ...