mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-22 03:24:57 +08:00
Remove unnecessary code (#4141)
This commit is contained in:
@ -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)
|
||||
);
|
||||
}
|
||||
|
@ -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];
|
||||
}
|
||||
|
@ -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];
|
||||
}
|
||||
}
|
||||
|
@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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]) {
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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];
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user