style: enabled InnerAssignment in checkstyle (#5162)

* style: enabled InnerAssignment in checkstyle

* Refactor code formatting in KnapsackMemoization.java and UnionFind.java

* style: remove redundant blank line

* style: mark `includeCurrentItem` and `excludeCurrentItem` as `final`

* style: remove `KnapsackMemoization` from `pmd-exclude.properties`

* style: use `final`

---------

Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
Godwill Christopher
2024-05-16 10:46:03 -06:00
committed by GitHub
parent f8e62fbb90
commit 0f42e995a4
15 changed files with 41 additions and 19 deletions

View File

@ -40,8 +40,15 @@ public class KnapsackMemoization {
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));
// case 1. include the item, if it is less than the capacity
final int includeCurrentItem = profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable);
// case 2. exclude the item if it is more than the capacity
final int excludeCurrentItem = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
// Store the value of function call stack in table and return
dpTable[numOfItems][capacity] = Math.max(includeCurrentItem, excludeCurrentItem);
return dpTable[numOfItems][capacity];
}
}
}

View File

@ -34,7 +34,8 @@ public final class LongestAlternatingSubsequence {
int[][] las = new int[n][2]; // las = LongestAlternatingSubsequence
for (int i = 0; i < n; i++) {
las[i][0] = las[i][1] = 1;
las[i][0] = 1;
las[i][1] = 1;
}
int result = 1; // Initialize result

View File

@ -15,7 +15,8 @@ public final class NewManShanksPrime {
public static boolean nthManShanksPrime(int n, int expected_answer) {
int[] a = new int[n + 1];
// array of n+1 size is initialized
a[0] = a[1] = 1;
a[0] = 1;
a[1] = 1;
// The 0th and 1st index position values are fixed. They are initialized as 1
for (int i = 2; i <= n; i++) {
a[i] = 2 * a[i - 1] + a[i - 2];