[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 ...
随机推荐
- STM32 CRC-32 Calculator Unit
AN4187 - Using the CRC peripheral in the STM32 family At start up, the algorithm sets CRC to the Ini ...
- Linux进程管理工具 Supervisord 的安装 及 入门教程
Supervisor是一个进程管理工具,官方的说法: 用途就是有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...
- man命令使用
如:man 2 read, 就可以查看read函数的文档
- Exynos4412的外部中断是如何安排的?
作者 彭东林 pengdonglin137@163.com 平台 Linux4.9 tiny4412 概述 结合tiny4412开发板分析一下Exynos4412的外部中断是如何组织的. ...
- Delphi的命令行编译命令
Borland出品的Delphi,有着闪电般的编译速度,但是在界面控件使用较多.工程项目较大的时候,编译一个工程仍需要一段时间,打开庞大的Delphi IDE,也需要时间.其实,在一个工程开发结束,调 ...
- EBS已安装模块
/* Formatted on 2018/3/15 11:14:51 (QP5 v5.256.13226.35538) */ SELECT fa.application_short_name , fp ...
- c++中 extern
用例子给你示范 // 1.cpp int x = 10; // 2.cpp 注意没有包含1.cpp #include <iostream> using namespace std; ext ...
- eclipse新发现功能之dos和terminal(ssh连接)
dos功能: window——>show view——>other——>remote systems,选择remote shell,选择确定或者双击,打开了一个新工具窗口. 点击re ...
- 都市侠盗第一季/全集Leverage迅雷下载
第一季 Leverage Season 1 (2008)看点:Nate Ford(蒂莫西·赫顿 Timothy Hutton 饰)曾当过保险调查员,为自己的老板挽回过数百万美元的损失,是个忠心耿耿的雇 ...
- tomcat配置jdbc
server.xml下<GlobalNamingResources> <Resource name="jdbc/Huobanplus" ...