Refactoring (#4146)
This commit is contained in:
parent
d241fafd64
commit
0c618b5ee8
@ -47,7 +47,8 @@ public class HamiltonianCycle {
|
||||
* @returns true if path is found false otherwise
|
||||
*/
|
||||
public boolean isPathFound(int vertex) {
|
||||
if (this.graph[vertex][0] == 1 && this.pathCount == this.V) {
|
||||
boolean isLastVertexConnectedToStart = this.graph[vertex][0] == 1 && this.pathCount == this.V;
|
||||
if (isLastVertexConnectedToStart) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -44,10 +44,14 @@ public class CheckTreeIsSymmetric {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (leftSubtreeRoot == null || rightSubtreRoot == null || leftSubtreeRoot.data != rightSubtreRoot.data) {
|
||||
if (isInvalidSubtree(leftSubtreeRoot, rightSubtreRoot)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isSymmetric(leftSubtreeRoot.right, rightSubtreRoot.left) && isSymmetric(leftSubtreeRoot.left, rightSubtreRoot.right);
|
||||
}
|
||||
|
||||
private static boolean isInvalidSubtree(Node leftSubtreeRoot, Node rightSubtreeRoot) {
|
||||
return leftSubtreeRoot == null || rightSubtreeRoot == null || leftSubtreeRoot.data != rightSubtreeRoot.data;
|
||||
}
|
||||
}
|
||||
|
@ -9,44 +9,43 @@ package com.thealgorithms.dynamicprogramming;
|
||||
*/
|
||||
public class KnapsackMemoization {
|
||||
|
||||
int knapSack(int W, int wt[], int val[], int N) {
|
||||
int knapSack(int capacity, int[] weights, int[] profits, int numOfItems) {
|
||||
|
||||
// Declare the table dynamically
|
||||
int dp[][] = new int[N + 1][W + 1];
|
||||
int[][] dpTable = new int[numOfItems + 1][capacity + 1];
|
||||
|
||||
// Loop to initially filled the
|
||||
// table with -1
|
||||
for (int i = 0; i < N + 1; i++) {
|
||||
for (int j = 0; j < W + 1; j++) {
|
||||
dp[i][j] = -1;
|
||||
// Loop to initially fill the table with -1
|
||||
for (int i = 0; i < numOfItems + 1; i++) {
|
||||
for (int j = 0; j < capacity + 1; j++) {
|
||||
dpTable[i][j] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return knapSackRec(W, wt, val, N, dp);
|
||||
return solveKnapsackRecursive(capacity, weights, profits, numOfItems, dpTable);
|
||||
}
|
||||
|
||||
// Returns the value of maximum profit using Recursive approach
|
||||
int knapSackRec(int W, int wt[],
|
||||
int val[], int n,
|
||||
int[][] dp) {
|
||||
// Returns the value of maximum profit using recursive approach
|
||||
int solveKnapsackRecursive(int capacity, int[] weights,
|
||||
int[] profits, int numOfItems,
|
||||
int[][] dpTable) {
|
||||
|
||||
// Base condition
|
||||
if (n == 0 || W == 0) {
|
||||
if (numOfItems == 0 || capacity == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (dp[n][W] != -1) {
|
||||
return dp[n][W];
|
||||
if (dpTable[numOfItems][capacity] != -1) {
|
||||
return dpTable[numOfItems][capacity];
|
||||
}
|
||||
|
||||
if (wt[n - 1] > W) {
|
||||
if (weights[numOfItems - 1] > capacity) {
|
||||
// Store the value of function call stack in table
|
||||
dp[n][W] = knapSackRec(W, wt, val, n - 1, dp);
|
||||
return dp[n][W];
|
||||
dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
|
||||
return dpTable[numOfItems][capacity];
|
||||
} else {
|
||||
// Return value of table after storing
|
||||
return dp[n][W] = Math.max((val[n - 1] + knapSackRec(W - wt[n - 1], wt, val, n - 1, dp)),
|
||||
knapSackRec(W, wt, val, n - 1, dp));
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user