mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-22 03:24:57 +08:00
style: enable LocalVariableName
in CheckStyle (#5191)
* style: enable LocalVariableName in checkstyle * Removed minor bug * Resolved Method Name Bug * Changed names according to suggestions
This commit is contained in:
@ -32,8 +32,8 @@ public final class BruteForceKnapsack {
|
||||
public static void main(String[] args) {
|
||||
int[] val = new int[] {60, 100, 120};
|
||||
int[] wt = new int[] {10, 20, 30};
|
||||
int W = 50;
|
||||
int w = 50;
|
||||
int n = val.length;
|
||||
System.out.println(knapSack(W, wt, val, n));
|
||||
System.out.println(knapSack(w, wt, val, n));
|
||||
}
|
||||
}
|
||||
|
@ -58,9 +58,9 @@ public final class CoinChange {
|
||||
for (int i = 1; i <= amount; i++) {
|
||||
for (int coin : coins) {
|
||||
if (coin <= i) {
|
||||
int sub_res = minimumCoins[i - coin];
|
||||
if (sub_res != Integer.MAX_VALUE && sub_res + 1 < minimumCoins[i]) {
|
||||
minimumCoins[i] = sub_res + 1;
|
||||
int subRes = minimumCoins[i - coin];
|
||||
if (subRes != Integer.MAX_VALUE && subRes + 1 < minimumCoins[i]) {
|
||||
minimumCoins[i] = subRes + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,13 +12,13 @@ public final class KadaneAlgorithm {
|
||||
|
||||
public static boolean maxSum(int[] a, int predicted_answer) {
|
||||
int sum = a[0];
|
||||
int running_sum = 0;
|
||||
int runningSum = 0;
|
||||
for (int k : a) {
|
||||
running_sum = running_sum + k;
|
||||
runningSum = runningSum + k;
|
||||
// running sum of all the indexs are stored
|
||||
sum = Math.max(sum, running_sum);
|
||||
sum = Math.max(sum, runningSum);
|
||||
// the max is stored inorder to the get the maximum sum
|
||||
if (running_sum < 0) running_sum = 0;
|
||||
if (runningSum < 0) runningSum = 0;
|
||||
// if running sum is negative then it is initialized to zero
|
||||
}
|
||||
// for-each loop is used to iterate over the array and find the maximum subarray sum
|
||||
|
@ -21,18 +21,18 @@ public final class LongestIncreasingSubsequence {
|
||||
}
|
||||
|
||||
public static int lis(int[] array) {
|
||||
int N = array.length;
|
||||
if (N == 0) {
|
||||
int len = array.length;
|
||||
if (len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int[] tail = new int[N];
|
||||
int[] tail = new int[len];
|
||||
|
||||
// always points empty slot in tail
|
||||
int length = 1;
|
||||
|
||||
tail[0] = array[0];
|
||||
for (int i = 1; i < N; i++) {
|
||||
for (int i = 1; i < len; i++) {
|
||||
// new smallest value
|
||||
if (array[i] < tail[0]) {
|
||||
tail[0] = array[i];
|
||||
|
@ -33,24 +33,24 @@ public final class PalindromicPartitioning {
|
||||
|
||||
int i;
|
||||
int j;
|
||||
int L; // different looping variables
|
||||
int subLen; // different looping variables
|
||||
|
||||
// Every substring of length 1 is a palindrome
|
||||
for (i = 0; i < len; i++) {
|
||||
isPalindrome[i][i] = true;
|
||||
}
|
||||
|
||||
/* L is substring length. Build the solution in bottom up manner by considering all
|
||||
/* subLen is substring length. Build the solution in bottom up manner by considering all
|
||||
* substrings of length starting from 2 to n. */
|
||||
for (L = 2; L <= len; L++) {
|
||||
// For substring of length L, set different possible starting indexes
|
||||
for (i = 0; i < len - L + 1; i++) {
|
||||
j = i + L - 1; // Ending index
|
||||
// If L is 2, then we just need to
|
||||
for (subLen = 2; subLen <= len; subLen++) {
|
||||
// For substring of length subLen, set different possible starting indexes
|
||||
for (i = 0; i < len - subLen + 1; i++) {
|
||||
j = i + subLen - 1; // Ending index
|
||||
// If subLen is 2, then we just need to
|
||||
// compare two characters. Else need to
|
||||
// check two corner characters and value
|
||||
// of P[i+1][j-1]
|
||||
if (L == 2) {
|
||||
if (subLen == 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];
|
||||
|
@ -6,13 +6,13 @@ final class ShortestSuperSequence {
|
||||
}
|
||||
|
||||
// Function to find length of the
|
||||
// shortest supersequence of X and Y.
|
||||
static int shortestSuperSequence(String X, String Y) {
|
||||
int m = X.length();
|
||||
int n = Y.length();
|
||||
// shortest supersequence of x and y.
|
||||
static int shortestSuperSequence(String x, String y) {
|
||||
int m = x.length();
|
||||
int n = y.length();
|
||||
|
||||
// find lcs
|
||||
int l = lcs(X, Y, m, n);
|
||||
int l = lcs(x, y, m, n);
|
||||
|
||||
// Result is sum of input string
|
||||
// lengths - length of lcs
|
||||
@ -20,39 +20,39 @@ final class ShortestSuperSequence {
|
||||
}
|
||||
|
||||
// Returns length of LCS
|
||||
// for X[0..m - 1], Y[0..n - 1]
|
||||
static int lcs(String X, String Y, int m, int n) {
|
||||
int[][] L = new int[m + 1][n + 1];
|
||||
// for x[0..m - 1], y[0..n - 1]
|
||||
static int lcs(String x, String y, int m, int n) {
|
||||
int[][] lN = new int[m + 1][n + 1];
|
||||
int i;
|
||||
int j;
|
||||
|
||||
// Following steps build L[m + 1][n + 1]
|
||||
// Following steps build lN[m + 1][n + 1]
|
||||
// in bottom up fashion. Note that
|
||||
// L[i][j] contains length of LCS
|
||||
// of X[0..i - 1]and Y[0..j - 1]
|
||||
// lN[i][j] contains length of lNCS
|
||||
// of x[0..i - 1]and y[0..j - 1]
|
||||
for (i = 0; i <= m; i++) {
|
||||
for (j = 0; j <= n; j++) {
|
||||
if (i == 0 || j == 0) {
|
||||
L[i][j] = 0;
|
||||
} else if (X.charAt(i - 1) == Y.charAt(j - 1)) {
|
||||
L[i][j] = L[i - 1][j - 1] + 1;
|
||||
lN[i][j] = 0;
|
||||
} else if (x.charAt(i - 1) == y.charAt(j - 1)) {
|
||||
lN[i][j] = lN[i - 1][j - 1] + 1;
|
||||
} else {
|
||||
L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]);
|
||||
lN[i][j] = Math.max(lN[i - 1][j], lN[i][j - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// L[m][n] contains length of LCS
|
||||
// for X[0..n - 1] and Y[0..m - 1]
|
||||
return L[m][n];
|
||||
// lN[m][n] contains length of LCS
|
||||
// for x[0..n - 1] and y[0..m - 1]
|
||||
return lN[m][n];
|
||||
}
|
||||
|
||||
// Driver code
|
||||
public static void main(String[] args) {
|
||||
String X = "AGGTAB";
|
||||
String Y = "GXTXAYB";
|
||||
String x = "AGGTAB";
|
||||
String y = "GXTXAYB";
|
||||
|
||||
System.out.println("Length of the shortest "
|
||||
+ "supersequence is " + shortestSuperSequence(X, Y));
|
||||
+ "supersequence is " + shortestSuperSequence(x, y));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user