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的更多相关文章

  1. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  2. UVALive 6908---Electric Bike(DP或记录型深搜)

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  3. Evacuation Plan-POJ2175最小费用消圈算法

    Time Limit: 1000MS Memory Limit: 65536K Special Judge Description The City has a number of municipal ...

  4. [最近公共祖先] POJ 3728 The merchant

    The merchant Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 4556   Accepted: 1576 Desc ...

  5. PTA week10

    // // main.c // Bonus2 // // Created by 余南龙 on 2016/11/27. // Copyright © 2016年 余南龙. All rights rese ...

  6. 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]中有多少个数 ...

  7. UOJ150 运输计划

    运输计划(transport.cpp/c/pas)[问题描述]公元 2044 年,人类进入了宇宙纪元.L 国有 n 个星球,还有 n-1 条 双向 航道,每条航道建立在两个星球之间,这 n-1 条航道 ...

  8. ORACLE SQL 分组

    select max(cost),suppliercode from tel_bill where period = '2014005' group by suppliercode;select * ...

  9. Canu Tutorial(canu指导手册)

    链接:Canu Tutorial Canu assembles reads from PacBio RS II or Oxford Nanopore MinION instruments into u ...

随机推荐

  1. pandas:对字符串类型做差分比较

    1. 问题需求 某种行为最常发生时段.最少发生时段与X天前是否一致 需求变形:判断上下行数据是否一致 2. 预备知识 2.1 Series.ne(Series) 判断两个Series是否相等 impo ...

  2. Famous框架系列一:famous/core/Surface

    famous/core/Surface 既然是Famous的第一篇文章,就先给Famous打个广告:http://periodic.famo.us  Famous12年的作品,点击右下角Fun thi ...

  3. cap原则(cap定理)与base理论

    CAP定理c:一致性 Consistency: 分布式系统中,所有数据备份,同一时刻存在一样的值.当在分布式环境中,当一个地方写入返回成功的结果,其他地方也应读取到最新的数据.a:可用性 Availa ...

  4. ModelForm解密

     一.复用model表和字段 models.py文件 class User(models.Model): username = models.CharField(max_length=32) emai ...

  5. Maven+spring+springMVC+mybatis+Junit+Log4j配置个人总结

          首先粘贴上项目引用地址:http://doc.okbase.net/fengshizty/archive/126397.html 这里对创建步骤不做过多解释,只是针对案例创建demo的一些 ...

  6. javaweb学习2——HTTP协议

    声明:本文只是自学过程中,记录自己不会的知识点的摘要,如果想详细学习JavaWeb,请到孤傲苍狼博客学习,JavaWeb学习点此跳转 本文链接:https://www.cnblogs.com/xdp- ...

  7. pygrib学习

    pygrib-2.0.3/docs/index.html 导入pygrib模块 >>> import pygrib 打开grib文件,获取grib消息迭代器 >>> ...

  8. 解决了一个困扰我近一年的vim显示中文乱码的问题

    今天解决了vi命令打开日志文件中文总是显示乱码的问题.由于项目组中的日志包含一些特殊字符,所以使用vim打开日志文件时总是不能正确识别出文件字符编码.此时用:set fileencoding命令可以看 ...

  9. 【转】Appium 中截取 element 图片作为对比,判断对比结果

    其实在https://github.com/gb112211/Adb-For-Test 里面有一个截取element进行对比的方法,但是在使用appium时是无法使用的,因为其用到了uiautomat ...

  10. [codeForce-1006C]-Three Parts of the Array (简单题)

    You are given an array d1,d2,…,dnd1,d2,…,dn consisting of nn integer numbers. Your task is to split ...