mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 03:59:38 +08:00
Refactoring (#4146)
This commit is contained in:
@ -47,7 +47,8 @@ public class HamiltonianCycle {
|
|||||||
* @returns true if path is found false otherwise
|
* @returns true if path is found false otherwise
|
||||||
*/
|
*/
|
||||||
public boolean isPathFound(int vertex) {
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,10 +44,14 @@ public class CheckTreeIsSymmetric {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leftSubtreeRoot == null || rightSubtreRoot == null || leftSubtreeRoot.data != rightSubtreRoot.data) {
|
if (isInvalidSubtree(leftSubtreeRoot, rightSubtreRoot)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return isSymmetric(leftSubtreeRoot.right, rightSubtreRoot.left) && isSymmetric(leftSubtreeRoot.left, rightSubtreRoot.right);
|
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 {
|
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
|
// 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
|
// Loop to initially fill the table with -1
|
||||||
// table with -1
|
for (int i = 0; i < numOfItems + 1; i++) {
|
||||||
for (int i = 0; i < N + 1; i++) {
|
for (int j = 0; j < capacity + 1; j++) {
|
||||||
for (int j = 0; j < W + 1; j++) {
|
dpTable[i][j] = -1;
|
||||||
dp[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
|
// Returns the value of maximum profit using recursive approach
|
||||||
int knapSackRec(int W, int wt[],
|
int solveKnapsackRecursive(int capacity, int[] weights,
|
||||||
int val[], int n,
|
int[] profits, int numOfItems,
|
||||||
int[][] dp) {
|
int[][] dpTable) {
|
||||||
|
|
||||||
// Base condition
|
// Base condition
|
||||||
if (n == 0 || W == 0) {
|
if (numOfItems == 0 || capacity == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dp[n][W] != -1) {
|
if (dpTable[numOfItems][capacity] != -1) {
|
||||||
return dp[n][W];
|
return dpTable[numOfItems][capacity];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wt[n - 1] > W) {
|
if (weights[numOfItems - 1] > capacity) {
|
||||||
// Store the value of function call stack in table
|
// Store the value of function call stack in table
|
||||||
dp[n][W] = knapSackRec(W, wt, val, n - 1, dp);
|
dpTable[numOfItems][capacity] = solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable);
|
||||||
return dp[n][W];
|
return dpTable[numOfItems][capacity];
|
||||||
} else {
|
} else {
|
||||||
// Return value of table after storing
|
// 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)),
|
return dpTable[numOfItems][capacity] = Math.max((profits[numOfItems - 1] + solveKnapsackRecursive(capacity - weights[numOfItems - 1], weights, profits, numOfItems - 1, dpTable)),
|
||||||
knapSackRec(W, wt, val, n - 1, dp));
|
solveKnapsackRecursive(capacity, weights, profits, numOfItems - 1, dpTable));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user