function greedy() {
var arr = [];
arr.push("[{act:'A'},{s:'0'},{e:'4'}]");
arr.push("[{act:'B'},{s:'1'},{e:'2'}]");
arr.push("[{act:'C'},{s:'2'},{e:'5'}]");
arr.push("[{act:'D'},{s:'3'},{e:'5'}]");
arr.push("[{act:'E'},{s:'2'},{e:'4'}]");
arr.push("[{act:'F'},{s:'4'},{e:'6'}]");
var sort = arr.sort(function (a, b) {
var aa = eval('(' + a + ')');
var bb = eval('(' + b + ')');
return aa[2].e + 0 > bb[2].e + 0 ? 1 : -1;
})
var rtn = [];
var current = eval('(' + arr[0] + ')');
var currentTime = current[2].e;
rtn.push(current[0].act);
for (var i = 1; i < arr.length; i++) {
var cur = eval('(' + arr[i] + ')');
if (currentTime <= cur[1].s) {
currentTime = cur[2].e;
rtn.push(cur[0].act);
}
}
return rtn;
}
贪心算法(活动安排)
最新推荐文章于 2023-09-22 18:56:10 发布