Skip to content

Commit a28977c

Browse files
committed
Improve comments
1 parent 1e8abf1 commit a28977c

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

src/main/java/com/thealgorithms/greedyalgorithms/ActivitySelection.java

+41-15
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,65 @@
44
import java.util.Arrays;
55
import java.util.Comparator;
66

7-
// Problem Link: https://en.wikipedia.org/wiki/Activity_selection_problem
7+
// Problem Link: https://en.wikipedia.org/wiki/Activity_selection_problem
88

99
public final class ActivitySelection {
10+
11+
// Private constructor to prevent instantiation of the utility class
1012
private ActivitySelection() {
1113
}
12-
// Function to perform activity selection
14+
15+
/**
16+
* Function to perform activity selection using a greedy approach.
17+
*
18+
* The goal is to select the maximum number of activities that don't overlap
19+
* with each other, based on their start and end times. Activities are chosen
20+
* such that no two selected activities overlap.
21+
*
22+
* @param startTimes Array containing the start times of the activities.
23+
* @param endTimes Array containing the end times of the activities.
24+
* @return A list of indices representing the selected activities that can be
25+
* performed without overlap.
26+
*/
1327
public static ArrayList<Integer> activitySelection(int[] startTimes, int[] endTimes) {
1428
int n = startTimes.length;
15-
int[][] activities = new int[n][3];
1629

17-
// Create a 2D array to store activities and their start/end times.
18-
// Each row: [activity index, start time, end time]
30+
// Create a 2D array to store activity indices along with their start and end
31+
// times.
32+
// Each row represents an activity in the format: [activity index, start time,
33+
// end time].
34+
int[][] activities = new int[n][3];
1935

36+
// Populate the 2D array with the activity index, start time, and end time.
2037
for (int i = 0; i < n; i++) {
21-
activities[i][0] = i; // Assign activity index
22-
activities[i][1] = startTimes[i]; // Assign start time
23-
activities[i][2] = endTimes[i]; // Assign end time
38+
activities[i][0] = i; // Assign the activity index
39+
activities[i][1] = startTimes[i]; // Assign the start time of the activity
40+
activities[i][2] = endTimes[i]; // Assign the end time of the activity
2441
}
2542

26-
// Sort activities by their end times in ascending order.
43+
// Sort activities based on their end times in ascending order.
44+
// This ensures that we always try to finish earlier activities first.
2745
Arrays.sort(activities, Comparator.comparingDouble(activity -> activity[2]));
28-
int lastEndTime;
46+
int lastEndTime; // Variable to store the end time of the last selected activity
47+
// List to store the indices of selected activities
2948
ArrayList<Integer> selectedActivities = new ArrayList<>();
30-
selectedActivities.add(activities[0][0]);
31-
lastEndTime = activities[0][2];
3249

33-
// Iterate through sorted activities to select compatible ones.
50+
// Select the first activity (as it has the earliest end time after sorting)
51+
selectedActivities.add(activities[0][0]); // Add the first activity index to the result
52+
lastEndTime = activities[0][2]; // Keep track of the end time of the last selected activity
53+
54+
// Iterate over the sorted activities to select the maximum number of compatible
55+
// activities.
3456
for (int i = 1; i < n; i++) {
57+
// If the start time of the current activity is greater than or equal to the
58+
// end time of the last selected activity, it means there's no overlap.
3559
if (activities[i][1] >= lastEndTime) {
36-
selectedActivities.add(activities[i][0]);
37-
lastEndTime = activities[i][2];
60+
selectedActivities.add(activities[i][0]); // Select this activity
61+
lastEndTime = activities[i][2]; // Update the end time of the last selected activity
3862
}
3963
}
64+
65+
// Return the list of selected activity indices.
4066
return selectedActivities;
4167
}
4268
}

0 commit comments

Comments
 (0)