mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-20 18:24:49 +08:00
Cleaned up code for some packages (#5094)
* Cleaned up code of some packages --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:

committed by
GitHub

parent
40cd4d86ef
commit
22310defcd
@ -8,9 +8,9 @@ import java.util.Comparator;
|
||||
|
||||
public class ActivitySelection {
|
||||
// Function to perform activity selection
|
||||
public static ArrayList<Integer> activitySelection(int startTimes[], int endTimes[]) {
|
||||
public static ArrayList<Integer> activitySelection(int[] startTimes, int[] endTimes) {
|
||||
int n = startTimes.length;
|
||||
int activities[][] = new int[n][3];
|
||||
int[][] activities = new int[n][3];
|
||||
|
||||
// Create a 2D array to store activities and their start/end times.
|
||||
// Each row: [activity index, start time, end time]
|
||||
|
@ -10,12 +10,11 @@ public class CoinChange {
|
||||
// Function to solve the coin change problem
|
||||
public static ArrayList<Integer> coinChangeProblem(int amount) {
|
||||
// Define an array of coin denominations in descending order
|
||||
Integer coins[] = {1, 2, 5, 10, 20, 50, 100, 500, 2000};
|
||||
Integer[] coins = {1, 2, 5, 10, 20, 50, 100, 500, 2000};
|
||||
|
||||
// Sort the coin denominations in descending order
|
||||
Arrays.sort(coins, Comparator.reverseOrder());
|
||||
|
||||
int count = 0; // Variable to keep track of the total number of coins used
|
||||
ArrayList<Integer> ans = new ArrayList<>(); // List to store selected coins
|
||||
|
||||
// Iterate through the coin denominations
|
||||
@ -24,7 +23,6 @@ public class CoinChange {
|
||||
if (coins[i] <= amount) {
|
||||
// Repeatedly subtract the coin denomination from the remaining amount
|
||||
while (coins[i] <= amount) {
|
||||
count++; // Increment the count of coins used
|
||||
ans.add(coins[i]); // Add the coin to the list of selected coins
|
||||
amount -= coins[i]; // Update the remaining amount
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ import java.util.Comparator;
|
||||
|
||||
public class FractionalKnapsack {
|
||||
// Function to perform fractional knapsack
|
||||
public static int fractionalKnapsack(int weight[], int value[], int capacity) {
|
||||
public static int fractionalKnapsack(int[] weight, int[] value, int capacity) {
|
||||
// Create a 2D array to store item indices and their value-to-weight ratios.
|
||||
double ratio[][] = new double[weight.length][2];
|
||||
double[][] ratio = new double[weight.length][2];
|
||||
|
||||
// Populate the ratio array with item indices and their value-to-weight ratios.
|
||||
for (int i = 0; i < weight.length; i++) {
|
||||
|
@ -31,7 +31,7 @@ public class JobSequencing {
|
||||
Boolean[] slots = new Boolean[size];
|
||||
Arrays.fill(slots, false);
|
||||
|
||||
int result[] = new int[size];
|
||||
int[] result = new int[size];
|
||||
|
||||
// Iterate through jobs to find the optimal job sequence
|
||||
for (int i = 0; i < size; i++) {
|
||||
|
Reference in New Issue
Block a user