最大化任务数量,以使第一个任务的开始时间始终是最早的
我的代码灵感来自以下 包含代码样本,该代码样本决定使用贪婪算法可以执行的最大活动数量。
关于贪婪的想法,我对问题的对待有所不同。我想从最早的开始时间开始,然后才能让贪婪的算法确定最佳解决方案(即从最早的开始时间开始的非重叠对)。
我正在使用PriortityQueue
,因为我将输入视为未分类。
我很想知道如何选择最早的开始时间的活动,并进行最大数量的非重叠对(活动)?
我的代码:
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Pair class
static class Pair {
int first;
int second;
Pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
static void SelectActivities(int s[], int f[])
{
// Vector to store results.
ArrayList<Pair> ans = new ArrayList<>();
// Minimum Priority Queue to sort activities in
// ascending order of finishing time (f[i]).
PriorityQueue<Pair> p = new PriorityQueue<>(
(p1, p2) -> p1.first - p2.first);
for (int i = 0; i < s.length; i++) {
// Pushing elements in priority queue where the
// key is f[i]
p.add(new Pair(f[i], s[i]));
}
Pair it = p.poll();
int start = it.second;
int end = it.first;
ans.add(new Pair(start, end));
while (!p.isEmpty()) {
Pair itr = p.poll();
if (itr.second >= end) {
start = itr.second;
end = itr.first;
ans.add(new Pair(start, end));
}
}
System.out.println(
"Following Activities should be selected. \n");
for (Pair itr : ans) {
System.out.println(
"Activity started at: " + itr.first
+ " and ends at " + itr.second);
}
}
// Driver Code
public static void main(String[] args)
{
int s[] = { 1, 3, 0, 5, 8, 5 };
int f[] = { 2, 4, 6, 7, 9, 9 };
// Function call
SelectActivities(s, f);
}
}
当前输出:
Following Activities should be selected.
Activity started at: 1 and ends at 2
Activity started at: 3 and ends at 4
Activity started at: 5 and ends at 7
Activity started at: 8 and ends at 9
所需的输出:
Following Activities should be selected.
Activity started at: 0 and ends at 6
Activity started at: 8 and ends at 9
My code is inspired by the following article which contains the code-sample which determines the maximum number of activities that can be performed using a greedy algorithm.
I am treating the problem differently with respect to the greedy idea. I would like to begin at the Earliest Start Time and only then let the greedy algorithm to determine the optimal solution (i.e. non-overlapping pairs beginning from the Earliest Start Time).
I'm using PriorityQueue
because I'm treating the input as unsorted.
I'm curious to know how to choose the activity having the Earliest Start Time and proceed with maximum number of non-overlapping pairs (activities)?
My code:
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Pair class
static class Pair {
int first;
int second;
Pair(int first, int second)
{
this.first = first;
this.second = second;
}
}
static void SelectActivities(int s[], int f[])
{
// Vector to store results.
ArrayList<Pair> ans = new ArrayList<>();
// Minimum Priority Queue to sort activities in
// ascending order of finishing time (f[i]).
PriorityQueue<Pair> p = new PriorityQueue<>(
(p1, p2) -> p1.first - p2.first);
for (int i = 0; i < s.length; i++) {
// Pushing elements in priority queue where the
// key is f[i]
p.add(new Pair(f[i], s[i]));
}
Pair it = p.poll();
int start = it.second;
int end = it.first;
ans.add(new Pair(start, end));
while (!p.isEmpty()) {
Pair itr = p.poll();
if (itr.second >= end) {
start = itr.second;
end = itr.first;
ans.add(new Pair(start, end));
}
}
System.out.println(
"Following Activities should be selected. \n");
for (Pair itr : ans) {
System.out.println(
"Activity started at: " + itr.first
+ " and ends at " + itr.second);
}
}
// Driver Code
public static void main(String[] args)
{
int s[] = { 1, 3, 0, 5, 8, 5 };
int f[] = { 2, 4, 6, 7, 9, 9 };
// Function call
SelectActivities(s, f);
}
}
Current output:
Following Activities should be selected.
Activity started at: 1 and ends at 2
Activity started at: 3 and ends at 4
Activity started at: 5 and ends at 7
Activity started at: 8 and ends at 9
Desired output:
Following Activities should be selected.
Activity started at: 0 and ends at 6
Activity started at: 8 and ends at 9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了始终从最早的任务开始,然后选择以使得满足任务的数量最大的方式选择其余任务,首先我们需要确定最早的任务,然后采用贪婪的方法来最大化任务总数。
问题可以在以下步骤中解决:
解析给定数组对并创建任务队列。
找到最早的任务并将其从队列中删除。
应用贪婪算法查找其余任务。
打印任务列表。
为了简明性表示 task ,我正在使用Java 16记录(您可以用类替代)。
这就是可以实现的方式:
输出:
In order to always start with the earliest task and then pick the rest tasks in such a way that the number of the fulfilled tasks would be maximal, firstly we need to determine the earliest task and then apply a greedy approach to maximize the total number of tasks.
The problem can be addressed in the following steps:
Parse the pair of given arrays and create a queue of tasks.
Find the earliest task and remove it from the queue.
Apply a greedy algorithm to find the rest tasks.
Print the list of tasks.
For conciseness to represent a task, I'm using Java 16 record (you can substitute with a class).
That's how it can be implemented:
Output:
它不是选择最早的开始时间吗?因为那是贪婪的概念。您要采取最早的赏金/奖励的最早任务。
Isn't it selecting the earliest start time already? Because that is the concept of Greedy. You take the earliest available task with the highest bounty/reward.