📜  作业排序问题 |第 3 组(在Java中使用 TreeSet)

📅  最后修改于: 2022-05-13 01:54:39.112000             🧑  作者: Mango

作业排序问题 |第 3 组(在Java中使用 TreeSet)

给定一组工作,其中每个工作都有截止日期和相关利润(如果工作在截止日期之前完成)。还假设每个工作都需要一个时间单位,因此任何工作的最小可能截止日期为 1。如果一次只能安排一个工作,如何使总利润最大化。

例子:

Input : Four Jobs with following deadlines and profits
  JobID    Deadline      Profit
    a        4            20   
    b        1            10
    c        1            40  
    d        1            30
Output : Following is maximum profit sequence of jobs
         c, a 
Input : Five Jobs with following deadlines and profits
   JobID     Deadline     Profit
     a         2           100
     b         1           19
     c         2           27
     d         1           25
     e         3           15
Output : Following is maximum profit sequence of jobs
         c, a, e

以下是在Java中使用 TreeSet 解决问题的分步算法:

  1. 将所有工作按照各自的利润按降序排列。
  2. 创建一个 TreeSet 并插入从 0 到 n-1 的所有整数。
  3. 遍历作业数组和第 i作业
    • 在 TreeSet 中搜索一个时间槽“x”,其最大值小于第 i作业的截止日期。
    • 如果存在任何值,则在答案中包含该作业并从 TreeSet 中删除“x”
    • 否则检查剩余的作业。

下面是上述算法的实现:

import java.io.*;
import java.util.*;
  
public class Solution {
      
    // Job class
    public static class Job {
        char id;
        int deadline;
        int profit;
  
        // Constructor
        Job(char id, int deadline, int profit)
        {
            this.id = id;
            this.deadline = deadline;
            this.profit = profit;
        }
    }
  
    public static class Sorted implements Comparator {
          
        // Function to implement comparator
        public int compare(Object o1, Object o2)
        {
            Job j1 = (Job)o1;
            Job j2 = (Job)o2;
  
            if (j1.profit != j2.profit)
                return j2.profit - j1.profit;
            else
                return j2.deadline - j1.deadline;
        }
    }
      
    // Function to print job scheduling
    public static void printJobScheduling(Job jobs[], int n)
    {
        // Creating object of Sorted class
        Sorted sorter = new Sorted();
          
        Arrays.sort(jobs, sorter);
      
        // Creating TreeSet Object
        TreeSet ts = new TreeSet<>();
  
        for (int i = 0; i < n; i++)
            ts.add(i);
  
        for (int i = 0; i < n; i++) {
            Integer x = ts.floor(jobs[i].deadline - 1);
  
            if (x != null) {
                System.out.print(jobs[i].id + " ");
                ts.remove(x);
            }
        }
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int n = 5;
        Job[] jobs = new Job[n];
  
        jobs[0] = new Job('a', 2, 100);
        jobs[1] = new Job('b', 1, 19);
        jobs[2] = new Job('c', 2, 27);
        jobs[3] = new Job('d', 1, 25);
        jobs[4] = new Job('e', 3, 15);
  
        printJobScheduling(jobs, n);
    }
    // Contributed by Dipesh Jain (dipesh_jain)
}

时间复杂度:O(N*log(N))
辅助空间:O(N)