📌  相关文章
📜  最大化可以在给定约束下完成的工作

📅  最后修改于: 2021-10-27 03:27:40             🧑  作者: Mango

给定一个表示作业数量的整数N和一个矩阵range[] ,该矩阵由需要完成的每个作业的范围[开始日,结束日] 组成,任务是找到可以完成的最大可能作业。
例子:

方法:上述问题可以使用优先队列来解决。请按照以下步骤解决问题:

  • 找出作业范围内的最小和最大天数。
  • 开始日的升序对所有作业进行排序。
  • 从最小天到最大天迭代,每i 天,选择当天可以完成的结束日期最少的作业。
  • 为了执行上述步骤,维护一个Min Heap ,并在每个i 天,将当天可以完成的作业插入到结束日期排序Min Heap 中。如果有任何的工作可以在i一天内完成,考虑一个最低的结束日期,并增加完成作业的数量。
  • 对所有天重复此过程,最后打印已完成的作业计数。

下面是上述方法的一个实现:

C++
// C++ Program to implement the
// above approach
 
#include 
using namespace std;
 
// Function to find maxiumum
// number of jobs
int find_maximum_jobs(
    int N,
    vector > ranges)
{
    // Min Heap
    priority_queue,
                   greater >
        queue;
 
    // Sort ranges by start day
    sort(ranges.begin(), ranges.end());
 
    // Stores the minimum and maximum
    // day in the ranges
    int min_day = ranges[0].first;
    int max_day = 0;
 
    for (int i = 0; i < N; i++)
        max_day
            = max(max_day,
                  ranges[i].second);
 
    int index = 0, count_jobs = 0;
 
    // Iterating from min_day to max_day
    for (int i = min_day; i <= max_day; i++) {
 
        // Insert the end day of the jobs
        // which can be completed on
        // i-th day in a priority queue
        while (index < ranges.size()
               && ranges[index].first <= i) {
 
            queue.push(ranges[index].second);
            index++;
        }
 
        // Pop all jobs whose end day
        // is less than current day
        while (!queue.empty()
               && queue.top() < i)
            queue.pop();
 
        // If queue is empty, no job
        // can be completed on
        // the i-th day
        if (queue.empty())
            continue;
 
        // Increment the count of
        // jobs completed
        count_jobs++;
 
        // Pop the job with
        // least end day
        queue.pop();
    }
 
    // Return the jobs
    // on the last day
    return count_jobs;
}
 
// Driver Code
int main()
{
 
    int N = 5;
    vector > ranges;
    ranges.push_back({ 1, 5 });
    ranges.push_back({ 1, 5 });
    ranges.push_back({ 1, 5 });
    ranges.push_back({ 2, 3 });
    ranges.push_back({ 2, 3 });
 
    cout << find_maximum_jobs(N, ranges);
}


Java
// Java Program to implement the
// above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
  // Function to find maxiumum
  // number of jobs
  static int find_maximum_jobs(int N, ArrayList> ranges)
  {
    // Min Heap
    ArrayList queue = new ArrayList();
    // Sort ranges by start day
    Collections.sort(ranges, new Comparator>() {   
      @Override
      public int compare(ArrayList o1, ArrayList o2) {
        return o1.get(0).compareTo(o2.get(0));
      }              
    });
 
    // Stores the minimum and maximum
    // day in the ranges
    int min_day = ranges.get(0).get(0);
    int max_day = 0;
    for (int i = 0; i < N; i++)
      max_day = Math.max(max_day, ranges.get(i).get(1));
    int index = 0, count_jobs = 0;
 
    // Iterating from min_day to max_day
    for (int i = min_day; i <= max_day; i++)
    {
      // Insert the end day of the jobs
      // which can be completed on
      // i-th day in a priority queue
      while (index < ranges.size() && ranges.get(index).get(0) <= i)
      {
        queue.add(ranges.get(index).get(1));
        index++;
      }
      Collections.sort(queue);
 
      // Pop all jobs whose end day
      // is less than current day
      while (queue.size() > 0 && queue.get(0) < i)
        queue.remove(0);
 
      // If queue is empty, no job
      // can be completed on
      // the i-th day
      if (queue.size() == 0)
        continue;
      // Increment the count of
      // jobs completed
      count_jobs++;
 
      // Pop the job with
      // least end day
      queue.remove(0);
    }
    // Return the jobs
    // on the last day
    return count_jobs;
 
  }
 
  // Driver code
  public static void main (String[] args) {
 
    int N = 5;
    ArrayList> ranges = new ArrayList>();
    ranges.add(new ArrayList(Arrays.asList(1, 5)));
    ranges.add(new ArrayList(Arrays.asList(1, 5)));
    ranges.add(new ArrayList(Arrays.asList(1, 5)));
    ranges.add(new ArrayList(Arrays.asList(2, 3)));
    ranges.add(new ArrayList(Arrays.asList(2, 3)));
    System.out.println(find_maximum_jobs(N, ranges));
  }
}
// This code is contributed by avanitrachhadiya2155


Python3
# Python3 Program to implement the
# above approach
 
# Function to find maxiumum
# number of jobs
def find_maximum_jobs(N, ranges) :
 
    # Min Heap
    queue = []
     
    # Sort ranges by start day
    ranges.sort()
     
    # Stores the minimum and maximum
    # day in the ranges
    min_day = ranges[0][0]
    max_day = 0
    for i in range(N) :
      max_day = max(max_day, ranges[i][1])
    index, count_jobs = 0, 0
     
    # Iterating from min_day to max_day
    for i in range(min_day, max_day + 1) :
     
      # Insert the end day of the jobs
      # which can be completed on
      # i-th day in a priority queue
      while (index < len(ranges) and ranges[index][0] <= i) :
       
        queue.append(ranges[index][1])
        index += 1
       
      queue.sort()
     
      # Pop all jobs whose end day
      # is less than current day
      while (len(queue) > 0 and queue[0] < i) :
        queue.pop(0)
     
      # If queue is empty, no job
      # can be completed on
      # the i-th day
      if (len(queue) == 0) :
        continue
     
      # Increment the count of
      # jobs completed
      count_jobs += 1
     
      # Pop the job with
      # least end day
      queue.pop(0)
     
    # Return the jobs
    # on the last day
    return count_jobs
     
    # Driver code
N = 5
ranges = []
ranges.append((1, 5))
ranges.append((1, 5))
ranges.append((1, 5))
ranges.append((2, 3))
ranges.append((2, 3))
 
print(find_maximum_jobs(N, ranges))
 
# This code is contributed by divyeshrabadiya07.


C#
// C# Program to implement the
// above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Function to find maxiumum
  // number of jobs
  static int find_maximum_jobs(int N, List> ranges)
  {
    // Min Heap
    List queue = new List();
 
    // Sort ranges by start day
    ranges.Sort();
 
    // Stores the minimum and maximum
    // day in the ranges
    int min_day = ranges[0].Item1;
    int max_day = 0;
    for (int i = 0; i < N; i++)
      max_day = Math.Max(max_day, ranges[i].Item2);
    int index = 0, count_jobs = 0;
 
    // Iterating from min_day to max_day
    for (int i = min_day; i <= max_day; i++)
    {
 
      // Insert the end day of the jobs
      // which can be completed on
      // i-th day in a priority queue
      while (index < ranges.Count && ranges[index].Item1 <= i)
      {
        queue.Add(ranges[index].Item2);
        index++;
      }
      queue.Sort();
 
      // Pop all jobs whose end day
      // is less than current day
      while (queue.Count > 0 && queue[0] < i)
        queue.RemoveAt(0);
 
      // If queue is empty, no job
      // can be completed on
      // the i-th day
      if (queue.Count == 0)
        continue;
 
      // Increment the count of
      // jobs completed
      count_jobs++;
 
      // Pop the job with
      // least end day
      queue.RemoveAt(0);
    }
 
    // Return the jobs
    // on the last day
    return count_jobs;
  }
 
  // Driver code
  static void Main()
  {
    int N = 5;
    List> ranges = new List>();
    ranges.Add(new Tuple(1, 5));
    ranges.Add(new Tuple(1, 5));
    ranges.Add(new Tuple(1, 5));
    ranges.Add(new Tuple(2, 3));
    ranges.Add(new Tuple(2, 3));
 
    Console.Write(find_maximum_jobs(N, ranges));
  }
}
 
// This code is contributed by divyesh072019.


输出:
5

时间复杂度: O(Xlog(N)),其中 X 是最大天数和最小天数之间的差值,N 是作业数。
辅助空间: O(N 2 )