Skip to content

Cleaned up code for some packages #5094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class IntegerToRoman {

private static int[] allArabianRomanNumbers = new int[] {
private static final int[] allArabianRomanNumbers = new int[] {
1000,
900,
500,
Expand All @@ -24,7 +24,7 @@ public class IntegerToRoman {
4,
1,
};
private static String[] allRomanNumbers = new String[] {
private static final String[] allRomanNumbers = new String[] {
"M",
"CM",
"D",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

public class RomanToInteger {

private static Map<Character, Integer> map = new HashMap<Character, Integer>() {
/**
* */
private static final Map<Character, Integer> map = new HashMap<>() {
private static final long serialVersionUID = 87605733047260530L;

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static String convertTurkishToLatin(String param) {
'G',
};
for (int i = 0; i < turkishChars.length; i++) {
param = param.replaceAll(new String(new char[] {turkishChars[i]}), new String(new char[] {latinChars[i]}));
param = param.replaceAll(String.valueOf(turkishChars[i]), String.valueOf(latinChars[i]));
}
return param;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class Fibonacci {

private static Map<Integer, Integer> map = new HashMap<>();
private static final Map<Integer, Integer> map = new HashMap<>();

public static void main(String[] args) {
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
Expand Down Expand Up @@ -106,7 +106,6 @@ public static int fibOptimized(int n) {
public static int fibBinet(int n) {
double squareRootOf5 = Math.sqrt(5);
double phi = (1 + squareRootOf5) / 2;
int nthTerm = (int) ((Math.pow(phi, n) - Math.pow(-phi, -n)) / squareRootOf5);
return nthTerm;
return (int) ((Math.pow(phi, n) - Math.pow(-phi, -n)) / squareRootOf5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

public class MatrixChainMultiplication {

private static Scanner scan = new Scanner(System.in);
private static ArrayList<Matrix> mArray = new ArrayList<>();
private static final Scanner scan = new Scanner(System.in);
private static final ArrayList<Matrix> mArray = new ArrayList<>();
private static int size;
private static int[][] m;
private static int[][] s;
Expand Down Expand Up @@ -115,9 +115,9 @@ private static String[] input(String string) {

class Matrix {

private int count;
private int col;
private int row;
private final int count;
private final int col;
private final int row;

Matrix(int count, int col, int row) {
this.count = count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +23,6 @@ public static ArrayList<Integer> coinChangeProblem(int amount) {
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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static String findJobSequence(ArrayList<Job> jobs, int size) {
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++) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/searches/UnionFind.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

public class UnionFind {

private int[] p;
private int[] r;
private final int[] p;
private final int[] r;

public UnionFind(int n) {
p = new int[n];
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/thealgorithms/sorts/DNFSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class DNFSort {
static void sort012(int[] a, int arr_size) {
int low = 0;
int high = arr_size - 1;
int mid = 0, temp = 0;
int mid = 0, temp;
while (mid <= high) {
switch (a[mid]) {
case 0: {
Expand Down Expand Up @@ -37,7 +37,7 @@ static void printArray(int[] arr, int arr_size) {
for (int i = 0; i < arr_size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
System.out.println();
}

/*Driver function to check for above functions*/
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/thealgorithms/strings/Pangram.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ public static void main(String[] args) {
*/
// alternative approach using Java Collection Framework
public static boolean isPangramUsingSet(String s) {
HashSet<Character> alpha = new HashSet<Character>();
HashSet<Character> alpha = new HashSet<>();
s = s.trim().toLowerCase();
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) != ' ') alpha.add(s.charAt(i));
if (alpha.size() == 26) return true;
return false;
return alpha.size() == 26;
}

/**
Expand Down