Remove unnecessary code (#4141)

This commit is contained in:
Saurabh Rahate
2023-04-03 20:05:59 +05:30
committed by GitHub
parent 805f09850c
commit ad72c28d91
64 changed files with 125 additions and 322 deletions

View File

@ -3,13 +3,6 @@ package com.thealgorithms.dynamicprogramming;
/* A Naive recursive implementation
of 0-1 Knapsack problem */
public class BruteForceKnapsack {
// A utility function that returns
// maximum of two integers
static int max(int a, int b) {
return (a > b) ? a : b;
}
// Returns the maximum value that
// can be put in a knapsack of
// capacity W
@ -29,8 +22,7 @@ public class BruteForceKnapsack {
// (1) nth item included
// (2) not included
else {
return max(
val[n - 1] + knapSack(W - wt[n - 1], 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)
);
}

View File

@ -3,11 +3,6 @@ package com.thealgorithms.dynamicprogramming;
// A Dynamic Programming based solution
// for 0-1 Knapsack problem
public class DyanamicProgrammingKnapsack {
static int max(int a, int b) {
return (a > b) ? a : b;
}
// Returns the maximum value that can
// be put in a knapsack of capacity W
static int knapSack(int W, int wt[], int val[], int n) {
@ -20,8 +15,7 @@ public class DyanamicProgrammingKnapsack {
if (i == 0 || w == 0) {
K[i][w] = 0;
} else if (wt[i - 1] <= w) {
K[i][w] =
max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
K[i][w] = Math.max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i - 1][w]);
} else {
K[i][w] = K[i - 1][w];
}

View File

@ -57,8 +57,8 @@ public class EditDistance {
int insert = dp[i][j + 1] + 1;
int delete = dp[i + 1][j] + 1;
int min = replace > insert ? insert : replace;
min = delete > min ? min : delete;
int min = Math.min(replace, insert);
min = Math.min(delete, min);
dp[i + 1][j + 1] = min;
}
}
@ -110,13 +110,12 @@ public class EditDistance {
if (s1.charAt(0) == s2.charAt(0)) {
storage[m][n] =
editDistance(s1.substring(1), s2.substring(1), storage);
return storage[m][n];
} else {
int op1 = editDistance(s1, s2.substring(1), storage);
int op2 = editDistance(s1.substring(1), s2, storage);
int op3 = editDistance(s1.substring(1), s2.substring(1), storage);
storage[m][n] = 1 + Math.min(op1, Math.min(op2, op3));
return storage[m][n];
}
return storage[m][n];
}
}

View File

@ -30,10 +30,7 @@ class LongestCommonSubsequence {
if (arr1[i - 1].equals(arr2[j - 1])) {
lcsMatrix[i][j] = lcsMatrix[i - 1][j - 1] + 1;
} else {
lcsMatrix[i][j] =
lcsMatrix[i - 1][j] > lcsMatrix[i][j - 1]
? lcsMatrix[i - 1][j]
: lcsMatrix[i][j - 1];
lcsMatrix[i][j] = Math.max(lcsMatrix[i - 1][j], lcsMatrix[i][j - 1]);
}
}
}

View File

@ -27,19 +27,9 @@ public class LongestPalindromicSubstring {
if (g == 0) {
arr[i][j] = true;
} else if (g == 1) {
if (input.charAt(i) == input.charAt(j)) {
arr[i][j] = true;
} else {
arr[i][j] = false;
}
arr[i][j] = input.charAt(i) == input.charAt(j);
} else {
if (
input.charAt(i) == input.charAt(j) && arr[i + 1][j - 1]
) {
arr[i][j] = true;
} else {
arr[i][j] = false;
}
arr[i][j] = input.charAt(i) == input.charAt(j) && arr[i + 1][j - 1];
}
if (arr[i][j]) {

View File

@ -87,7 +87,7 @@ public class MatrixChainMultiplication {
private static void printArray(int[][] array) {
for (int i = 1; i < size + 1; i++) {
for (int j = 1; j < size + 1; j++) {
System.out.print(String.format("%7d", array[i][j]));
System.out.printf("%7d", array[i][j]);
}
System.out.println();
}

View File

@ -48,27 +48,21 @@ public class PalindromicPartitioning {
if (L == 2) {
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j));
} else {
if (
(word.charAt(i) == word.charAt(j)) &&
isPalindrome[i + 1][j - 1]
) {
isPalindrome[i][j] = true;
} else {
isPalindrome[i][j] = false;
}
isPalindrome[i][j] = (word.charAt(i) == word.charAt(j)) &&
isPalindrome[i + 1][j - 1];
}
}
}
//We find the minimum for each index
for (i = 0; i < len; i++) {
if (isPalindrome[0][i] == true) {
if (isPalindrome[0][i]) {
minCuts[i] = 0;
} else {
minCuts[i] = Integer.MAX_VALUE;
for (j = 0; j < i; j++) {
if (
isPalindrome[j + 1][i] == true &&
isPalindrome[j + 1][i] &&
1 + minCuts[j] < minCuts[i]
) {
minCuts[i] = 1 + minCuts[j];

View File

@ -112,7 +112,7 @@ public class RegexMatching {
return true;
}
if (strg[svidx][pvidx] != 0) {
return strg[svidx][pvidx] == 1 ? false : true;
return strg[svidx][pvidx] != 1;
}
char chs = src.charAt(svidx);
char chp = pat.charAt(pvidx);
@ -127,7 +127,7 @@ public class RegexMatching {
} else {
ans = false;
}
strg[svidx][pvidx] = ans == false ? 1 : 2;
strg[svidx][pvidx] = ans ? 2 : 1;
return ans;
}

View File

@ -24,9 +24,7 @@ public class WineProblem {
int start = WPRecursion(arr, si + 1, ei) + arr[si] * year;
int end = WPRecursion(arr, si, ei - 1) + arr[ei] * year;
int ans = Math.max(start, end);
return ans;
return Math.max(start, end);
}
// Method 2: Top-Down DP(Memoization)