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 ...
随机推荐
- USACO Section1.2
section1.1主要包括四道题和两个编程知识介绍.下面将对这6个部分内容进行学习. Your Ride Is Here 这道题没什么难度,读懂题目意思就行:把两个字符串按照题目要求转换成数字,然后 ...
- MYSQL之视图、触发器、事务
一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...
- html5 初试 indexedDB
indexedDB是存储大量结构化数据的API,demo中用到的是异步API,麻烦的就是所有对indexedDB的操作都会发生一个异步的‘请求’,只要熟悉了API操作起来也很简单. http://ww ...
- X5webview完美去掉分享功能和缓存功能(2)
前段时间比较忙,没有来得及写完如何将X5WEBVIEW分享功能和缓存功能屏蔽,下面直接来干货,上代码. 1.首先在布局文件中增加一个全屏的布局, <!-- 视频全屏--> <Fram ...
- springboot 前后端分离开发 从零到整(三、登录以及登录状态的持续)
今天来写一下怎么登录和维持登录状态. 相信登录验证大家都比较熟悉,在Javaweb中一般保持登录状态都会用session.但如果是前后端分离的话,session的作用就没有那么明显了.对于前后端分离的 ...
- PHPCMS增加SEO字段调用
alter table v9_site add site_title_index varchar(255) not null;alter table v9_site add keywords_ind ...
- kubeadm源码修改证书时间 -1.13
编译后~ 链接:https://pan.baidu.com/s/1ofLX1Sv0ZF2yjkJdqf-6rw 提取码:cnbd 已统一与CA证书都是10年 已测试 适用于k8s 1.10 至 1.1 ...
- React Native移动开发实战-5-Android平台的调试技巧
Android平台的调试和其他平台的调试也很类似,例如:在Android Studio打开的工程中,打开源码MainActivity.java,然后,将鼠标移至代码编辑区的左侧后,单击鼠标即可添加断点 ...
- React Native移动开发实战-2-如何调试React Native项目
在实际开发中,还有一个影响开发效率的重要因素:调试. 在1.4.3节中已经介绍了Enable Live Debugger的使用.本节来介绍另一个非常重要的调试选项:Debug JSRemotely选项 ...
- 温习DL之一:梯度的概念
1.梯度的概念 梯度是一个矢量,表示某一函数在该点处的方向导数沿着该方向取得最大值,即函数在该点处沿着该方向变化最快. 在微积分里面,对多元函数的参数求∂偏导数,把求得的各个参数的偏导数以向量的形式写 ...