|
4 | 4 | import java.util.Arrays; |
5 | 5 | import java.util.Comparator; |
6 | 6 |
|
7 | | -// Problem Link: https://en.wikipedia.org/wiki/Activity_selection_problem |
| 7 | +// Problem Link: https://en.wikipedia.org/wiki/Activity_selection_problem |
8 | 8 |
|
9 | 9 | public final class ActivitySelection { |
| 10 | + |
| 11 | + // Private constructor to prevent instantiation of the utility class |
10 | 12 | private ActivitySelection() { |
11 | 13 | } |
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 | + */ |
13 | 27 | public static ArrayList<Integer> activitySelection(int[] startTimes, int[] endTimes) { |
14 | 28 | int n = startTimes.length; |
15 | | - int[][] activities = new int[n][3]; |
16 | 29 |
|
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]; |
19 | 35 |
|
| 36 | + // Populate the 2D array with the activity index, start time, and end time. |
20 | 37 | 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 |
24 | 41 | } |
25 | 42 |
|
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. |
27 | 45 | 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 |
29 | 48 | ArrayList<Integer> selectedActivities = new ArrayList<>(); |
30 | | - selectedActivities.add(activities[0][0]); |
31 | | - lastEndTime = activities[0][2]; |
32 | 49 |
|
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. |
34 | 56 | 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. |
35 | 59 | 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 |
38 | 62 | } |
39 | 63 | } |
| 64 | + |
| 65 | + // Return the list of selected activity indices. |
40 | 66 | return selectedActivities; |
41 | 67 | } |
42 | 68 | } |
0 commit comments