Skip to content

Commit 0bd86b3

Browse files
authored
Improve comments in ActivitySelection.java (#5485)
1 parent 7f60d57 commit 0bd86b3

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

DIRECTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@
560560
* [LongestNonRepetitiveSubstring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestNonRepetitiveSubstring.java)
561561
* [LongestPalindromicSubstring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/LongestPalindromicSubstring.java)
562562
* [Lower](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Lower.java)
563+
* [Manacher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Manacher.java)
563564
* [MyAtoi](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/MyAtoi.java)
564565
* [Palindrome](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Palindrome.java)
565566
* [Pangram](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Pangram.java)
@@ -846,6 +847,8 @@
846847
* [TwinPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java)
847848
* [VolumeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/VolumeTest.java)
848849
* misc
850+
* [ColorContrastRatioTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ColorContrastRatioTest.java)
851+
* [InverseOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/InverseOfMatrixTest.java)
849852
* [MapReduceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MapReduceTest.java)
850853
* [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
851854
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
@@ -857,6 +860,7 @@
857860
* [ArrayRightRotation](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotation.java)
858861
* [ArrayRightRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayRightRotationTest.java)
859862
* [BestFitCPUTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BestFitCPUTest.java)
863+
* [BFPRTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BFPRTTest.java)
860864
* [BoyerMooreTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/BoyerMooreTest.java)
861865
* cn
862866
* [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/cn/HammingDistanceTest.java)
@@ -976,6 +980,7 @@
976980
* [LongestNonRepetitiveSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestNonRepetitiveSubstringTest.java)
977981
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LongestPalindromicSubstringTest.java)
978982
* [LowerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/LowerTest.java)
983+
* [ManacherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ManacherTest.java)
979984
* [MyAtoiTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/MyAtoiTest.java)
980985
* [PalindromeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PalindromeTest.java)
981986
* [PangramTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/PangramTest.java)

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)