-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSortedActivitySelectionProblem.java
More file actions
73 lines (57 loc) · 1.98 KB
/
SortedActivitySelectionProblem.java
File metadata and controls
73 lines (57 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// http://www.geeksforgeeks.org/greedy-algorithms-set-1-activity-selection-problem/
/*
Problem - You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
Solution - The greedy choice is to always pick the next activity whose finish time is least among the remaining activities and the start time is more than or equal to the finish time of previously selected activity.
Algorithmic Approach - Greedy Algorithm
Assumption - start and finish are sorted according to finish time
Time Complexity - O(N)
Space Complexity - O(1)
*/
import java.util.LinkedList;
class ActivitySelectionProblem {
public static void main(String[] args) {
int[] start = new int[] {1, 3, 0, 5, 8, 5};
int[] finish = new int[] {2, 4, 6, 7, 9, 9};
LinkedList<Integer> indexList = new LinkedList<>();
LinkedList<StartFinish> activityList = new LinkedList<>();
int count = 0;
int last_finish_index = 0;
if(start.length > 0) {
count++;
indexList.add(0);
activityList.add(new StartFinish(start[0], finish[0]));
}
for(int i = 1; i < finish.length; i++) {
if(start[i] >= finish[last_finish_index]) {
indexList.add(i);
activityList.add(new StartFinish(start[i], finish[i]));
count++;
last_finish_index = i;
}
}
System.out.println("Num of activities - " + count);
System.out.print("Index of selected activities - ");
for(Integer i : indexList) {
System.out.print(i + " ");
}
System.out.println();
System.out.print("Start finish of selected activities - ");
for(StartFinish i : activityList) {
System.out.print("{ " + i.getStart() + ", " + i.getFinish() + " } ");
}
}
}
class StartFinish {
int start;
int finish;
StartFinish(int start, int finish) {
this.start = start;
this.finish = finish;
}
public int getStart() {
return this.start;
}
public int getFinish() {
return this.finish;
}
}