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. JAVA 对象序列化(二)——Externalizable

    Java默认的序列化机制非常简单,而且序列化后的对象不需要再次调用构造器重新生成,但是在实际中,我们可以会希望对象的某一部分不需要被序列化,或者说一个对象被还原之后,其内部的某些子对象需要重新创建,从 ...

  2. 解决SQL Server 2008提示评估期已过

    第一步:进入SQL2008配置工具中的安装中心第二步:再进入维护界面,选择版本升级第三步:进入产品密钥,输入密钥第四步:一直点下一步,直到升级完毕.SQL Server 2008 Developer: ...

  3. 集合框架的类和接口均在java.util包中。 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换。

    集合框架的类和接口均在java.util包中. 任何对象加入集合类后,自动转变为Object类型,所以在取出的时候,需要进行强制类型转换.

  4. 【转】iPhone易被窃听应用三分钟即可获取所有信息

    2011年8月9日10:19 “你有iPhone吗?这下你麻烦了!”昨天香港<东方日报>封面文章用这样的语气报道说,一种iPhone等智能手机窃听程序,正引爆香港. 该报记者亲自试验,发现 ...

  5. C#编程(六十八)----------LINQ小结

    LINQ小结 一.LINQ是什么 LINQ也就是Language Interrated Query的缩写,怎么一个缩写法我也不明白,即语言集成查询,是微软在.NET3.5中提出的一项新技术,LINQ主 ...

  6. clientX, clientY,offsetX, offsetY,screenX, screenY, x, y

    clientX, clientY是鼠标当前相对于网页的位置,当鼠标位于页面左上角时clientX=0, clientY=0: offsetX, offsetY是鼠标当前相对于网页中的某一区域的位置,当 ...

  7. 使用PHP+Sphinx建立高效的站内搜索引擎

      1.    为什么要使用Sphinx   假设你现在运营着一个论坛,论坛数据已经超过100W,很多用户都反映论坛搜索的速度非常慢,那么这时你就可以考虑使用Sphinx了(当然其他的全文检索程序或方 ...

  8. Spring+Velocity(平台升级至Spring Framework 5.0.2)

    下载: http://repo.spring.io/release/org/springframework/spring/ Dear Spring community, I’m pleased to ...

  9. 识骨寻踪第一季/全集Bones迅雷下载

    第一季 Bones Season 1 (2005)看点:这是一部专门从“骨头”上寻找破案线索的刑侦剧.女博士布莱南绰号“骨头”(艾米丽·丹斯切尔 Emily Deschanel 饰),是个学识渊博.专 ...

  10. 解决Installation error: INSTALL_FAILED_VERSION_DOWNGRADE错误

    Installation error: INSTALL_FAILED_VERSION_DOWNGRADE 说明你手机里已经装的软件版本比你要安装的软件版本要高,所以不能安装. 你只要删除你安装的应用便 ...