For example we have array of meeting objects:

const data = [
{ name: "m1", hours: },
{ name: "m2", hours: },
{ name: "m3", hours: },
{ name: "m4", hours: },
{ name: "m5", hours: }
];

For a day, 8 hours, we want to take as any meetings as possible:

const res = optimizeMeetings(data, );

You should write function 'optimizeMeetings', get the results of selected meetings to attend.

This problem is the same as Knapack problem, we can construct a table:

hours / total 1 2 3 4 5 6 7 8
2  0  2  2  2  2  2  2
4  0  2  2  4  4  6  6  6
3  0  2  3  4  5  6  7 7
3  0  2  3  4  5  6  7  8
1  1  2  3  4  5  6  7 8

The max hours we can take is the last row & col value, which in the end should be 8.

Then we should trace back the table to find which items should be included.

Final Code:

/**@description
* When we have our result for Knapsack problem, we want to back trace to get the selected items.
*
* What we need to do is trace form last item of the memo, moving up
*/
const backTrace = (hours, totalHours, memo) => {
function helper(memo, row, col) {
let current = memo[row][col];
let selected = [];
while (current >= && row >= && col >= ) {
// If we reach the first row, then check whether we have the remaining?
// If yes then we need to add this row item into final result
if (row === && current !== ) {
selected.push(row);
break;
} let sameRowPrevCol = memo[row][col - ];
let prevRowSameCol = memo[row - ][col]; if (current !== sameRowPrevCol && prevRowSameCol !== current) {
// Item should be selected if the value with sibling values are differnet
selected.push(row);
// calcuate the remaining
col = current - hours[row] - ;
row = row - ;
} else if (prevRowSameCol === current && current !== sameRowPrevCol) {
// current is coming from previous row with the same column, reset row
row = row - ;
} else if (current === sameRowPrevCol && prevRowSameCol !== current) {
// current is coming from previous column with the same row, reset column
col = col - ;
}
// Update current with new row and new column
current = memo[row][col];
}
return selected;
} return helper(memo, hours.length - , totalHours.length - );
}; const getMaxHours = (hours, totalHours) => {
let memo = [...new Array(hours.length)].map(
x => new Array(totalHours.length)
);
function helper(hours, totalHours, memo) {
for (let row in hours) {
const value = hours[row];
for (let col in totalHours) {
// Fill in the first row
if (!memo[row - ]) {
memo[row][col] = value <= totalHours[col] ? value : ;
continue;
} // if the current value is larger than constrain, we use previous value
const prevRowSameCol = memo[row - ][col];
if (value > totalHours[col]) {
memo[row][col] = prevRowSameCol;
continue;
} // if the current value is equal to constrain, then Max{value, prevRowSameCol}
if (value === totalHours[col]) {
memo[row][col] = Math.max(value, prevRowSameCol);
} // if the current value is smaller than constrain
// Math {value + memo[row - 1][diff]: where diff is constrain-value, prevRowSameCol}
if (value < totalHours[col]) {
const diff = totalHours[col] - value - ;
memo[row][col] = Math.max(
prevRowSameCol,
value + memo[row - ][diff]
);
}
}
}
return memo;
}
memo = helper(hours, totalHours, memo);
const selectedIndex = backTrace(hours, totalHours, memo); return {
memo,
selectedIndex
};
}; function* genearteNumberAry(start, num) {
let i = start;
while (i <= num) {
yield i;
i++;
}
} /**
* Main
*/
/**
* @param meetings: [{name: string, hours: number}]
* @param haveHours: number
*
* @returns [meetings]
*/
function optimizeMeetings(meetings, haveHours) {
const hours = meetings.map(m => m.hours);
const haveHoursAry = Array.from(genearteNumberAry(, haveHours));
const { selectedIndex } = getMaxHours(hours, haveHoursAry);
return selectedIndex.map(i => meetings[i]);
} const data = [
{ name: "m1", hours: },
{ name: "m2", hours: },
{ name: "m3", hours: },
{ name: "m4", hours: },
{ name: "m5", hours: }
]; const res = optimizeMeetings(data, );
console.log(JSON.stringify(res)); // [{"name":"m4","hours":3},{"name":"m3","hours":3},{"name":"m1","hours":2}]

[Algorithm] Meeting hour optimization (Kanpsack problem) and Dynamic programming的更多相关文章

  1. Codeforces 1503C Travelling Salesman Problem(Dynamic Programming)

    题意 大家都是优秀生,这点英文还是看得懂的:点此看题 题解 由于旅行路线成一个环,所以从哪里出发不重要,我们把景点按照 a i a_i ai​ 排序,不妨就从左边最小的出发.基础的旅行费用 c i c ...

  2. hdu 4223 Dynamic Programming?

    Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  3. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

  4. [Optimization] Dynamic programming

    “就是迭代,被众人说得这么玄乎" “之所以归为优化,是因为动态规划本质是一个systemetic bruce force" “因为systemetic,所以比穷举好了许多,就认为是 ...

  5. [Optimization] Advanced Dynamic programming

    这里主要是较为详细地理解动态规划的思想,思考一些高质量的案例,同时也响应如下这么一句口号: “迭代(regression)是人,递归(recursion)是神!” Video series for D ...

  6. HDU-4972 A simple dynamic programming problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...

  7. 以计算斐波那契数列为例说说动态规划算法(Dynamic Programming Algorithm Overlapping subproblems Optimal substructure Memoization Tabulation)

    动态规划(Dynamic Programming)是求解决策过程(decision process)最优化的数学方法.它的名字和动态没有关系,是Richard Bellman为了唬人而取的. 动态规划 ...

  8. 最优化问题 Optimization Problems & 动态规划 Dynamic Programming

    2018-01-12 22:50:06 一.优化问题 优化问题用数学的角度来分析就是去求一个函数或者说方程的极大值或者极小值,通常这种优化问题是有约束条件的,所以也被称为约束优化问题. 约束优化问题( ...

  9. [Algorithms] Using Dynamic Programming to Solve longest common subsequence problem

    Let's say we have two strings: str1 = 'ACDEB' str2 = 'AEBC' We need to find the longest common subse ...

随机推荐

  1. lamp 5.6.36 bug记录

    后来发现另一个问题,php文字水印中文是乱码. 用yum安装lamp环境详见:https://blog.csdn.net/u010071211/article/details/80370201 在ce ...

  2. bzoj3456 城市规划 多项式求In

    \(n\)个点的无向联通图的个数 打着好累啊 一定要封装一个板子 记\(C(x)\)为无向图个数的指数型生成函数,\(C(0) = 1\) 记\(G(x)\)为无向联通图个数的指数型生成函数,\(G( ...

  3. bzoj 1312 最大密度子图

    晕,m=0是要输出1(弄的我还找管理员要数据,但明显题意是叫我们输出0呀) 最大密度子图,把边转换成点,然后二分答案,跑最大权闭合子图判定是否可行. #include <cstdio> # ...

  4. ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS

    B. Om Nom and Dark Park Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  5. Codeforces Round #294 (Div. 2)D - A and B and Interesting Substrings 字符串

    D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 megaby ...

  6. LNMP环境下打击那搭建Bugfree

    说明: LNMP: L=linx;N= Nginx:M=MySQL:p=PHP 本文分四部分进行讲解说明: 1.Nginx的搭建:2.php-fpm安装配置:3.源码安装Mysql5.6版本的数据库: ...

  7. 多线程面试题-sleep()和wait()区别

    sleep(): 1 .是Thread类中的一个方法,用于线程休眠, 2 .休眠时间结束后,该线程可以自动唤醒: wait(): 1. 是Object类中的一个方法,用于线程等待, 2. 如果想要唤醒 ...

  8. KVM资源划分分配技巧

    kvm有个叫做超分的概念,根据这个特性可以分配出超出物理机配置的数台虚拟机. 以下是自己总结的一些划分技巧: 一.最保守方法(性能最好) 根据物理机的资源,按虚拟机的数量叠加但不超过物理机的总和.不超 ...

  9. Shell脚本里的双冒号是什么意思

    这个是代码开发风格,其实也就是一个函数名,相当于下划线分割,但改读成包名之后就意义不一样.这个是根据Google的Shell开发规范进行定义的. 参考: https://google.github.i ...

  10. Linux下生成随机密码(转)

    1.使用SHA算法来加密日期,并输出结果的前32个字符: date +%s |sha256sum |base64 |head -c 32 ;echo 生成结果如下: ZTNiMGM0NDI5OGZjM ...