• 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,则需要存成二维。

     1 package leetcode;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Arrays;
     5 import java.util.Comparator;
     6 
     7 class Job{
     8     Integer start_time;
     9     Integer end_time;
    10     Integer cost;
    11     public Job(Integer s, Integer e, Integer c){
    12         start_time = s;
    13         end_time = e;
    14         cost = c;
    15     }
    16     
    17     public String toString(){
    18         StringBuilder sb = new StringBuilder();
    19         sb.append("Job: start [" + start_time + "], end ["+ end_time + "], cost [" + cost + "];");
    20         return sb.toString();
    21     }
    22 }
    23 
    24 public class FindNonOverlapJobs {
    25     public static ArrayList<Job> findJobsWithMaxCost(Job[] jobList){
    26         ArrayList<Job> result = new ArrayList<Job> ();
    27         if(jobList == null || jobList.length == 0) return result;
    28         Arrays.sort(jobList, new Comparator<Job>(){
    29             public int compare(Job j1, Job j2){
    30                 return j1.end_time > j2.end_time ? 1 : (j1.end_time == j2.end_time ? 0 : -1);
    31             }
    32         });
    33         int len = jobList.length;
    34         int[][] dp = new int[len + 1][jobList[len - 1].end_time + 1];
    35         for(int i = 1; i <= len; i ++){
    36             Job tmp = jobList[i - 1];
    37             int start = tmp.start_time;
    38             int end = tmp.end_time;
    39             for(int j = 0; j < dp[0].length; j ++){
    40                 if(j < end){
    41                     dp[i][j] = dp[i - 1][j];
    42                 }else if(j == end){
    43                     dp[i][j] = Math.max(dp[i - 1][start] + tmp.cost, dp[i - 1][j]);
    44                 }else{
    45                     dp[i][j] = dp[i][j - 1];
    46                 }
    47             }
    48         }
    49 
    50         int i = dp[0].length - 1;
    51         while(i > 0){
    52             if(dp[len][i] == dp[len][i - 1]) i --;
    53             else{
    54                 int j = len;
    55                 while(j > 0 && dp[j][i] == dp[j - 1][i]) j --;
    56                 result.add(jobList[j - 1]);
    57                 i --;
    58             }
    59         }
    60         return result;
    61     }
    62     
    63 public static void main(String[] args){
    64     Job[] test = new Job[5];
    65     test[0] = new Job(1,3,4);
    66     test[1] = new Job(3,5,2);
    67     test[2] = new Job(2,3,3);
    68     test[3] = new Job(1,2,2);
    69     test[4] = new Job(2,6,3);
    70     ArrayList<Job> result = findJobsWithMaxCost(test);
    71     for(int i = 0; i < result.size(); i ++){
    72         System.out.println(result.get(i).toString());
    73     }
    74 }
    75 }

    Output:

    Job: start [3], end [5], cost [2];

    Job: start [2], end [3], cost [3];

    Job: start [1], end [2], cost [2];

  • 相关阅读:
    如何识别思科胖瘦AP
    路由器重置用户名密码
    python学习之路day09(黏包、md5和进程守护)
    python学习之路day02(容器类型和循环)
    python学习之路08(正则表达式和网络)
    python学习之路day06(模块+面向对象)
    python学习之路day05(迭代器和生成器)
    python学习之路day04(函数)
    【Linux】ssh远程时如何做到不用输入密码登入
    【中间件】GitLab安装
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/4390148.html
Copyright © 2020-2023  润新知