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. 【BZOJ1044】[HAOI2008]木棍分割

    [BZOJ1044][HAOI2008]木棍分割 题面 bzoj 洛谷 题解 第一问显然可以二分出来的. 第二问: 设\(dp[i][j]\)表示前\(i\)个,切了\(j\)组的方案数 发现每次转移 ...

  2. 初探JSP运行机制和与Servlet间的关系

    自己看的书,手动画的图,如果有错误,请指正,谢谢.

  3. 新建一个Java Web程序

    依次选择 File——New——Web——Dynamic Web Project 输入项目名称“MyWebProject”,选择好Apache Tomcat V9.0服务器,其他采用默认配置. 单击N ...

  4. shell 本地接口自动化

    一.基于http/https的接口 一般情况下,当前大多公司在做接口自动化的时候都会使用一些工具:比如:postman/jmeter/python自研开发接口平台... 以上的情况,都是在源码与测试使 ...

  5. 宝塔中mysql数据库命名小坑

    今天在通过宝塔新建网站,添加mysql数据库,名字中间有下划线,发现能够创建成功,但是实际链接后,是没有这个数据库的.是宝塔的原因还是liunx服务器的原因? 不支持下划线的数据库名字吗? 比如 bo ...

  6. FFMS2 API 译文 [原创]

    FFMS2 又称 FFmpegSource2,参阅 https://github.com/FFMS/ffms2. 原文:https://github.com/FFMS/ffms2/blob/maste ...

  7. Cocos2dx源码赏析(1)之启动流程与主循环

    Cocos2dx源码赏析(1)之启动流程与主循环 我们知道Cocos2dx是一款开源的跨平台游戏引擎,而学习开源项目一个较实用的办法就是读源码.所谓,"源码之前,了无秘密".而笔者 ...

  8. tf导出pb文件,以及如何使用pb文件

    先罗列出来代码,有时间再解释 from tensorflow.python.framework import graph_util import tensorflow as tf def export ...

  9. EOS博彩合约设计

    集中博彩游戏合约设计 一.功能接口 1. 质押deposit 由用户发起,用户将个人账户中token质押给平台,从而可以进入平台去参与平台活动. 2. 赎回withdraw 由用户发起,在用户结束平台 ...

  10. LINUX开发使用的3个远程工具

    1,SecureCRT 2,SSH Secure Shell Client 3,VNC Viewer 如果想VNC Server启动时加载vncserver服务 需要修改/etc/rc.d/rc.lo ...