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 ...
随机推荐
- 微信小程序:实现日历功能
一.功能描述 实现日历功能 二. 代码实现 1. index.wxml <view class='wrap'> <view> <view class='date-show ...
- ASP.NET MVC Bundles 之学习笔记
在网页中,我们经常需要引用大量的javascript和css文件,在加上许多javascript库都包含debug版和经过压缩的release版(比如jquery),不仅麻烦还很容易引起混乱,所以AS ...
- 第四节:Windows系统安装时BIOS设置及注意
BIOS系统 BIOS是英文"Basic Input Output System"的缩略词,直译过来后中文名称就是"基本输入输出系统".在IBM PC兼容系统上 ...
- react学习(一)组件
react这个东西,说实话,我刚刚接触一个月不到.感觉这玩意很颠覆我以前的前端开发 比方说,可能,整个项目,并没有一个html文件 比方说,以前我们写前端代码,分的清清楚楚,html里面就是放dom, ...
- UGUI实现不规则区域点击响应
UGUI实现不规则区域点击响应 前言 大家吼啊!最近工作上事情特别多,没怎么打理博客.今天无意打开cnblog才想起该写点东西了.今天给大家讲一个Unity中不规则区域点击响应的实现方法,使用UGUI ...
- 关于IPointerEnterHandler和IpointerExitHandler的简单说明
自己在学习UGUI背包系统时用到了,而且遇到一些问题,所以在这里写一下这两个接口的简单说明. 继承IPointerEnterHandler和IpointerExitHandler这两个接口后可以实现两 ...
- JDK 和 JRE 有什么区别
JDK是Java开发工具包(Java Development Kit),JRE是Java运行环境(Java Runtime Environment),JDK包含了JRE,搭建Java环境的时候,安装J ...
- 2.4 Oracle之DCL的SQL语句之用户权限以及三大范式
DCL (Data Control Language,数据库控制语言)用于定义数据库权限 一.用户权限 1.1 建立用户以及授权: Eg :CREATE USER 用户名 IDENTIFIED ...
- git push remote: User permission denied
这种错误因为本地保存了一个错误的账号密码,只需要重新编辑成正确的账号密码 直接上方法
- PHP的垃圾回收
PHP使用引用计数和写时拷贝(Copy-On-Write)来管理内存. 引用技术不言自明,写时拷贝工作原来如下: $worker = array("Fred", 35, " ...