Find non-overlap jobs with max cost
Given a set of n jobs with [start time, end time, cost] find a subset so that no 2 jobs overlap and the cost is maximum.
Job: (start_time, end_time] --- cost
如果只是求maxCost, 一维就可以做。
但是如果要知道有选了哪些job,则需要存成二维。
package leetcode; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator; class Job{
Integer start_time;
Integer end_time;
Integer cost;
public Job(Integer s, Integer e, Integer c){
start_time = s;
end_time = e;
cost = c;
} public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Job: start [" + start_time + "], end ["+ end_time + "], cost [" + cost + "];");
return sb.toString();
}
} public class FindNonOverlapJobs {
public static ArrayList<Job> findJobsWithMaxCost(Job[] jobList){
ArrayList<Job> result = new ArrayList<Job> ();
if(jobList == null || jobList.length == 0) return result;
Arrays.sort(jobList, new Comparator<Job>(){
public int compare(Job j1, Job j2){
return j1.end_time > j2.end_time ? 1 : (j1.end_time == j2.end_time ? 0 : -1);
}
});
int len = jobList.length;
int[][] dp = new int[len + 1][jobList[len - 1].end_time + 1];
for(int i = 1; i <= len; i ++){
Job tmp = jobList[i - 1];
int start = tmp.start_time;
int end = tmp.end_time;
for(int j = 0; j < dp[0].length; j ++){
if(j < end){
dp[i][j] = dp[i - 1][j];
}else if(j == end){
dp[i][j] = Math.max(dp[i - 1][start] + tmp.cost, dp[i - 1][j]);
}else{
dp[i][j] = dp[i][j - 1];
}
}
} int i = dp[0].length - 1;
while(i > 0){
if(dp[len][i] == dp[len][i - 1]) i --;
else{
int j = len;
while(j > 0 && dp[j][i] == dp[j - 1][i]) j --;
result.add(jobList[j - 1]);
i --;
}
}
return result;
} public static void main(String[] args){
Job[] test = new Job[5];
test[0] = new Job(1,3,4);
test[1] = new Job(3,5,2);
test[2] = new Job(2,3,3);
test[3] = new Job(1,2,2);
test[4] = new Job(2,6,3);
ArrayList<Job> result = findJobsWithMaxCost(test);
for(int i = 0; i < result.size(); i ++){
System.out.println(result.get(i).toString());
}
}
}
Output:
Job: start [3], end [5], cost [2];
Job: start [2], end [3], cost [3];
Job: start [1], end [2], cost [2];
Find non-overlap jobs with max cost的更多相关文章
- POJ 2516 Minimum Cost (费用流)
题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...
- UVALive 6908---Electric Bike(DP或记录型深搜)
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- Evacuation Plan-POJ2175最小费用消圈算法
Time Limit: 1000MS Memory Limit: 65536K Special Judge Description The City has a number of municipal ...
- [最近公共祖先] POJ 3728 The merchant
The merchant Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 4556 Accepted: 1576 Desc ...
- PTA week10
// // main.c // Bonus2 // // Created by 余南龙 on 2016/11/27. // Copyright © 2016年 余南龙. All rights rese ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- UOJ150 运输计划
运输计划(transport.cpp/c/pas)[问题描述]公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n-1 条 双向 航道,每条航道建立在两个星球之间,这 n-1 条航道 ...
- ORACLE SQL 分组
select max(cost),suppliercode from tel_bill where period = '2014005' group by suppliercode;select * ...
- Canu Tutorial(canu指导手册)
链接:Canu Tutorial Canu assembles reads from PacBio RS II or Oxford Nanopore MinION instruments into u ...
随机推荐
- 【轮子狂魔】WeChatAPI 开源系统架构详解
如果使用WeChatAPI,它扮演着什么样的角色? 从图中我们可以看到主要分为3个部分: 1.业务系统 2.WeChatAPI: WeChatWebAPI,主要是接收微信服务器请求: WeChatAP ...
- 使用html2canvas将html标签转化为图片
有些时候我们无法用常规的截图工具截取网页内容,这时可以尝试以下方法: 1.首先允许跨域,Chrome浏览器可以在快捷方式->属性->目标栏里添加: --disable-web-securi ...
- SQL查询语句大全及其理解
转自:https://www.cnblogs.com/1234abcd/p/5530314.html 一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删 ...
- hover时显示可跟随鼠标移动的浮动框,运用函数节流与去抖进行优化
在很多笔试面试题中总能看到js函数去抖和函数节流,看过很多关于这两者的讨论,最近终于在一个需求中使用了函数去抖(debounce)和函数节流(throttle). 需要完成的效果是,鼠标在表格的单元格 ...
- SQL Server存储过程用法介绍
存储过程其实就是已预编译为可执行过程的一个或多个SQL语句. 通过调用和传递参数即可完成该存储过程的功能. 前面有介绍过存储过程的一些语法,但是没有详细示例,今天我们来一起研究一下存储过程. 提高性能 ...
- 程序员应该懂的ip地址知识汇总
1.A类ip由1字节(1字节是8位2进制数)的网络地址和3字节的主机地址组成,网络地址最高位必须是0,地址范围是从1.0.0.0到126.0.0.0,所以A类网络地址有126个,每个网络能容纳至少2^ ...
- 【视频编解码·学习笔记】13. 提取PPS信息程序
PPS结构解析 与之前解析SPS方式类似 一.定义PPS类: 在3.NAL Unit目录下,新建PicParamSet.cpp和PicParamSet.h,在这两个文件中写入类的定义和函数实现. 类定 ...
- 查看linux端口对应的进程id
例如:查看占用4040端口的进程 ss -lptn 'sport = :4040'
- linux 其他知识目录
博客目录总纲首页 为博客园添加目录的方法总结 linux 命令自动补全包 手动配置网卡 nginx日志统计 Linux 深入理解inode/block/superblock /proc/sys目录下各 ...
- 前端_JQuery
使用参考:http://jquery.cuishifeng.cn/ 目录 jQuery是什么 jQuery对象 寻找元素(选择器和筛选器) 选择器 表单属性选择器 筛选器 操作元素(属性.css.文档 ...