mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-22 03:24:57 +08:00
Add automatic linter (#4214)
This commit is contained in:
@ -24,8 +24,7 @@ public class BoundaryFill {
|
||||
* @param x_co_ordinate The x co-ordinate at which color is to be filled
|
||||
* @param y_co_ordinate The y co-ordinate at which color is to be filled
|
||||
*/
|
||||
public static void putPixel(
|
||||
int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color) {
|
||||
public static void putPixel(int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color) {
|
||||
image[x_co_ordinate][y_co_ordinate] = new_color;
|
||||
}
|
||||
|
||||
@ -38,11 +37,8 @@ public class BoundaryFill {
|
||||
* @param new_color The new color which to be filled in the image
|
||||
* @param boundary_color The old color which is to be replaced in the image
|
||||
*/
|
||||
public static void boundaryFill(
|
||||
int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color, int boundary_color) {
|
||||
if (x_co_ordinate >= 0 && y_co_ordinate >= 0
|
||||
&& getPixel(image, x_co_ordinate, y_co_ordinate) != new_color
|
||||
&& getPixel(image, x_co_ordinate, y_co_ordinate) != boundary_color) {
|
||||
public static void boundaryFill(int[][] image, int x_co_ordinate, int y_co_ordinate, int new_color, int boundary_color) {
|
||||
if (x_co_ordinate >= 0 && y_co_ordinate >= 0 && getPixel(image, x_co_ordinate, y_co_ordinate) != new_color && getPixel(image, x_co_ordinate, y_co_ordinate) != boundary_color) {
|
||||
putPixel(image, x_co_ordinate, y_co_ordinate, new_color);
|
||||
boundaryFill(image, x_co_ordinate + 1, y_co_ordinate, new_color, boundary_color);
|
||||
boundaryFill(image, x_co_ordinate - 1, y_co_ordinate, new_color, boundary_color);
|
||||
|
@ -22,8 +22,7 @@ public class BruteForceKnapsack {
|
||||
// (1) nth item included
|
||||
// (2) not included
|
||||
else {
|
||||
return Math.max(
|
||||
val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1), knapSack(W, wt, val, n - 1));
|
||||
return Math.max(val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1), knapSack(W, wt, val, n - 1));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,10 +10,8 @@ public class CoinChange {
|
||||
int amount = 12;
|
||||
int[] coins = {2, 4, 5};
|
||||
|
||||
System.out.println("Number of combinations of getting change for " + amount
|
||||
+ " is: " + change(coins, amount));
|
||||
System.out.println("Minimum number of coins required for amount :" + amount
|
||||
+ " is: " + minimumCoins(coins, amount));
|
||||
System.out.println("Number of combinations of getting change for " + amount + " is: " + change(coins, amount));
|
||||
System.out.println("Minimum number of coins required for amount :" + amount + " is: " + minimumCoins(coins, amount));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -76,8 +76,7 @@ public class EditDistance {
|
||||
s2 = input.nextLine();
|
||||
// ans stores the final Edit Distance between the two strings
|
||||
int ans = minDistance(s1, s2);
|
||||
System.out.println(
|
||||
"The minimum Edit Distance between \"" + s1 + "\" and \"" + s2 + "\" is " + ans);
|
||||
System.out.println("The minimum Edit Distance between \"" + s1 + "\" and \"" + s2 + "\" is " + ans);
|
||||
input.close();
|
||||
}
|
||||
|
||||
|
@ -25,8 +25,7 @@ public class KnapsackMemoization {
|
||||
}
|
||||
|
||||
// Returns the value of maximum profit using recursive approach
|
||||
int solveKnapsackRecursive(
|
||||
int capacity, int[] weights, int[] profits, int numOfItems, int[][] dpTable) {
|
||||
int solveKnapsackRecursive(int capacity, int[] weights, int[] profits, int numOfItems, int[][] dpTable) {
|
||||
// Base condition
|
||||
if (numOfItems == 0 || capacity == 0) {
|
||||
return 0;
|
||||
@ -38,16 +37,11 @@ public class KnapsackMemoization {
|
||||
|
||||
if (weights[numOfItems - 1] > capacity) {
|
||||
// Store the value of function call stack in table
|
||||
dpTable[numOfItems][capacity]
|
||||
= solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
|
||||
dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
|
||||
return dpTable[numOfItems][capacity];
|
||||
} else {
|
||||
// Return value of table after storing
|
||||
return dpTable[numOfItems][capacity]
|
||||
= Math.max((profits[numOfItems - 1]
|
||||
+ solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights,
|
||||
profits, numOfItems - 1, dpTable)),
|
||||
solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable));
|
||||
return dpTable[numOfItems][capacity] = Math.max((profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable)), solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,9 +32,7 @@ public class LevenshteinDistance {
|
||||
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
|
||||
distanceMat[i][j] = distanceMat[i - 1][j - 1];
|
||||
} else {
|
||||
distanceMat[i][j] = 1
|
||||
+ minimum(distanceMat[i - 1][j], distanceMat[i - 1][j - 1],
|
||||
distanceMat[i][j - 1]);
|
||||
distanceMat[i][j] = 1 + minimum(distanceMat[i - 1][j], distanceMat[i - 1][j - 1], distanceMat[i][j - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,8 +32,7 @@ public class LongestPalindromicSubsequence {
|
||||
} else {
|
||||
// if the last chars match, then remove it from both strings and recur
|
||||
if (original.charAt(original.length() - 1) == reverse.charAt(reverse.length() - 1)) {
|
||||
String bestSubResult = recursiveLPS(original.substring(0, original.length() - 1),
|
||||
reverse.substring(0, reverse.length() - 1));
|
||||
String bestSubResult = recursiveLPS(original.substring(0, original.length() - 1), reverse.substring(0, reverse.length() - 1));
|
||||
|
||||
bestResult = reverse.charAt(reverse.length() - 1) + bestSubResult;
|
||||
} else {
|
||||
@ -42,10 +41,8 @@ public class LongestPalindromicSubsequence {
|
||||
// updated original and reverse again then select the best result from these two
|
||||
// subproblems.
|
||||
|
||||
String bestSubResult1
|
||||
= recursiveLPS(original, reverse.substring(0, reverse.length() - 1));
|
||||
String bestSubResult2
|
||||
= recursiveLPS(original.substring(0, original.length() - 1), reverse);
|
||||
String bestSubResult1 = recursiveLPS(original, reverse.substring(0, reverse.length() - 1));
|
||||
String bestSubResult2 = recursiveLPS(original.substring(0, original.length() - 1), reverse);
|
||||
if (bestSubResult1.length() > bestSubResult2.length()) {
|
||||
bestResult = bestSubResult1;
|
||||
} else {
|
||||
|
@ -28,8 +28,7 @@ public class MatrixChainRecursiveTopDownMemoisation {
|
||||
return m[i][j];
|
||||
} else {
|
||||
for (int k = i; k < j; k++) {
|
||||
int q = Lookup_Chain(m, p, i, k) + Lookup_Chain(m, p, k + 1, j)
|
||||
+ (p[i - 1] * p[k] * p[j]);
|
||||
int q = Lookup_Chain(m, p, i, k) + Lookup_Chain(m, p, k + 1, j) + (p[i - 1] * p[k] * p[j]);
|
||||
if (q < m[i][j]) {
|
||||
m[i][j] = q;
|
||||
}
|
||||
|
@ -25,8 +25,7 @@ public class OptimalJobScheduling {
|
||||
* @param Transfer ,M*M symmetric matrix refers to the transportation delay for each pair of
|
||||
* machines
|
||||
*/
|
||||
public OptimalJobScheduling(
|
||||
int numberProcesses, int numberMachines, int[][] Run, int[][] Transfer) {
|
||||
public OptimalJobScheduling(int numberProcesses, int numberMachines, int[][] Run, int[][] Transfer) {
|
||||
this.numberProcesses = numberProcesses;
|
||||
this.numberMachines = numberMachines;
|
||||
this.Run = Run;
|
||||
@ -75,15 +74,13 @@ public class OptimalJobScheduling {
|
||||
return Run[process][machine];
|
||||
else {
|
||||
|
||||
int[] runningCosts
|
||||
= new int[numberMachines]; // stores the costs of executing our Process depending on
|
||||
// the Machine the previous one was executed
|
||||
int[] runningCosts = new int[numberMachines]; // stores the costs of executing our Process depending on
|
||||
// the Machine the previous one was executed
|
||||
|
||||
for (int k = 0; k < numberMachines; k++) // computes the cost of executing the previous
|
||||
// process to each and every Machine
|
||||
runningCosts[k] = Cost[process - 1][k] + Transfer[k][machine]
|
||||
+ Run[process][machine]; // transferring the result to our Machine and executing
|
||||
// the Process to our Machine
|
||||
runningCosts[k] = Cost[process - 1][k] + Transfer[k][machine] + Run[process][machine]; // transferring the result to our Machine and executing
|
||||
// the Process to our Machine
|
||||
|
||||
return findMin(runningCosts); // returns the minimum running cost
|
||||
}
|
||||
|
@ -49,8 +49,7 @@ public class PalindromicPartitioning {
|
||||
if (L == 2) {
|
||||
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j));
|
||||
} else {
|
||||
isPalindrome[i][j]
|
||||
= (word.charAt(i) == word.charAt(j)) && isPalindrome[i + 1][j - 1];
|
||||
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j)) && isPalindrome[i + 1][j - 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -81,8 +80,7 @@ public class PalindromicPartitioning {
|
||||
word = input.nextLine();
|
||||
// ans stores the final minimal cut count needed for partitioning
|
||||
int ans = minimalpartitions(word);
|
||||
System.out.println(
|
||||
"The minimum cuts needed to partition \"" + word + "\" into palindromes is " + ans);
|
||||
System.out.println("The minimum cuts needed to partition \"" + word + "\" into palindromes is " + ans);
|
||||
input.close();
|
||||
}
|
||||
}
|
||||
|
@ -159,8 +159,7 @@ public class RegexMatching {
|
||||
String pat = "*";
|
||||
System.out.println("Method 1: " + regexRecursion(src, pat));
|
||||
System.out.println("Method 2: " + regexRecursion(src, pat, 0, 0));
|
||||
System.out.println(
|
||||
"Method 3: " + regexRecursion(src, pat, 0, 0, new int[src.length()][pat.length()]));
|
||||
System.out.println("Method 3: " + regexRecursion(src, pat, 0, 0, new int[src.length()][pat.length()]));
|
||||
System.out.println("Method 4: " + regexBU(src, pat));
|
||||
}
|
||||
}
|
||||
|
@ -75,8 +75,7 @@ public class WineProblem {
|
||||
public static void main(String[] args) {
|
||||
int[] arr = {2, 3, 5, 1, 4};
|
||||
System.out.println("Method 1: " + WPRecursion(arr, 0, arr.length - 1));
|
||||
System.out.println(
|
||||
"Method 2: " + WPTD(arr, 0, arr.length - 1, new int[arr.length][arr.length]));
|
||||
System.out.println("Method 2: " + WPTD(arr, 0, arr.length - 1, new int[arr.length][arr.length]));
|
||||
System.out.println("Method 3: " + WPBU(arr));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user