Formatted with Google Java Formatter

This commit is contained in:
github-actions
2020-10-24 10:23:28 +00:00
parent a23bac99e8
commit 5d59a2e828
219 changed files with 13758 additions and 14582 deletions

View File

@ -1,8 +1,8 @@
package DynamicProgramming;
/*
* this is an important Algo in which
* we have starting and ending of board and we have to reach
* we have to count no. of ways
* this is an important Algo in which
* we have starting and ending of board and we have to reach
* we have to count no. of ways
* that help to reach end point i.e number by rolling dice
* which have 1 to 6 digits
@ -20,58 +20,56 @@ int n=10;
startAlgo();
System.out.println(bpIS(0,n,strg));
System.out.println(endAlgo()+"ms");
*/
public class BoardPath {
public static long startTime;
public static long endTime;
public static void startAlgo() {
startTime=System.currentTimeMillis();
}
public static long endAlgo() {
endTime=System.currentTimeMillis();
return endTime-startTime;
}
public static int bpR(int start,int end){
if(start==end) {
return 1;
}
else if(start>end)
return 0;
int count=0;
for(int dice=1;dice<=6;dice++) {
count+=bpR(start+dice,end);
}
return count;
}
public static int bpRS(int curr,int end,int strg[]){
if(curr==end) {
return 1;
}
else if(curr>end)
return 0;
if(strg[curr]!=0)
return strg[curr];
int count=0;
for(int dice=1;dice<=6;dice++) {
count+=bpRS(curr+dice,end,strg);
}
strg[curr]=count;
return count;
}
public static int bpIS(int curr,int end,int[]strg){
strg[end]=1;
for(int i=end-1;i>=0;i--) {
int count=0;
for(int dice=1;dice<=6&&dice+i<strg.length;dice++) {
count+=strg[i+dice];
}
strg[i]=count;
}
return strg[0];
}
public static long startTime;
public static long endTime;
public static void startAlgo() {
startTime = System.currentTimeMillis();
}
public static long endAlgo() {
endTime = System.currentTimeMillis();
return endTime - startTime;
}
public static int bpR(int start, int end) {
if (start == end) {
return 1;
} else if (start > end) return 0;
int count = 0;
for (int dice = 1; dice <= 6; dice++) {
count += bpR(start + dice, end);
}
return count;
}
public static int bpRS(int curr, int end, int strg[]) {
if (curr == end) {
return 1;
} else if (curr > end) return 0;
if (strg[curr] != 0) return strg[curr];
int count = 0;
for (int dice = 1; dice <= 6; dice++) {
count += bpRS(curr + dice, end, strg);
}
strg[curr] = count;
return count;
}
public static int bpIS(int curr, int end, int[] strg) {
strg[end] = 1;
for (int i = end - 1; i >= 0; i--) {
int count = 0;
for (int dice = 1; dice <= 6 && dice + i < strg.length; dice++) {
count += strg[i + dice];
}
strg[i] = count;
}
return strg[0];
}
}

View File

@ -1,79 +1,82 @@
package DynamicProgramming;
/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
/** @author Varun Upadhyay (https://github.com/varunu28) */
public class CoinChange {
// Driver Program
public static void main(String[] args) {
// Driver Program
public static void main(String[] args) {
int amount = 12;
int[] coins = {2, 4, 5};
int amount = 12;
int[] coins = {2, 4, 5};
System.out.println("Number of combinations of getting change for " + amount + " is: " + change(coins, amount));
System.out.println("Minimum number of coins required for amount :" + amount + " is: " + minimumCoins(coins, amount));
System.out.println(
"Number of combinations of getting change for " + amount + " is: " + change(coins, amount));
System.out.println(
"Minimum number of coins required for amount :"
+ amount
+ " is: "
+ minimumCoins(coins, amount));
}
/**
* This method finds the number of combinations of getting change for a given amount and change
* coins
*
* @param coins The list of coins
* @param amount The amount for which we need to find the change Finds the number of combinations
* of change
*/
public static int change(int[] coins, int amount) {
int[] combinations = new int[amount + 1];
combinations[0] = 1;
for (int coin : coins) {
for (int i = coin; i < amount + 1; i++) {
combinations[i] += combinations[i - coin];
}
// Uncomment the below line to see the state of combinations for each coin
// printAmount(combinations);
}
/**
* This method finds the number of combinations of getting change for a given amount and change coins
*
* @param coins The list of coins
* @param amount The amount for which we need to find the change
* Finds the number of combinations of change
**/
public static int change(int[] coins, int amount) {
return combinations[amount];
}
int[] combinations = new int[amount + 1];
combinations[0] = 1;
/**
* This method finds the minimum number of coins needed for a given amount.
*
* @param coins The list of coins
* @param amount The amount for which we need to find the minimum number of coins. Finds the the
* minimum number of coins that make a given value.
*/
public static int minimumCoins(int[] coins, int amount) {
// minimumCoins[i] will store the minimum coins needed for amount i
int[] minimumCoins = new int[amount + 1];
for (int coin : coins) {
for (int i = coin; i < amount + 1; i++) {
combinations[i] += combinations[i - coin];
}
// Uncomment the below line to see the state of combinations for each coin
// printAmount(combinations);
}
minimumCoins[0] = 0;
return combinations[amount];
for (int i = 1; i <= amount; i++) {
minimumCoins[i] = Integer.MAX_VALUE;
}
/**
* This method finds the minimum number of coins needed for a given amount.
*
* @param coins The list of coins
* @param amount The amount for which we need to find the minimum number of coins.
* Finds the the minimum number of coins that make a given value.
**/
public static int minimumCoins(int[] coins, int amount) {
//minimumCoins[i] will store the minimum coins needed for amount i
int[] minimumCoins = new int[amount + 1];
minimumCoins[0] = 0;
for (int i = 1; i <= amount; i++) {
minimumCoins[i] = Integer.MAX_VALUE;
for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if (coin <= i) {
int sub_res = minimumCoins[i - coin];
if (sub_res != Integer.MAX_VALUE && sub_res + 1 < minimumCoins[i])
minimumCoins[i] = sub_res + 1;
}
for (int i = 1; i <= amount; i++) {
for (int coin : coins) {
if (coin <= i) {
int sub_res = minimumCoins[i - coin];
if (sub_res != Integer.MAX_VALUE && sub_res + 1 < minimumCoins[i])
minimumCoins[i] = sub_res + 1;
}
}
}
// Uncomment the below line to see the state of combinations for each coin
//printAmount(minimumCoins);
return minimumCoins[amount];
}
}
// Uncomment the below line to see the state of combinations for each coin
// printAmount(minimumCoins);
return minimumCoins[amount];
}
// A basic print method which prints all the contents of the array
public static void printAmount(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
// A basic print method which prints all the contents of the array
public static void printAmount(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
}

View File

@ -1,79 +1,80 @@
package DynamicProgramming;
/**
* A DynamicProgramming based solution for Edit Distance problem In Java
* Description of Edit Distance with an Example:
* A DynamicProgramming based solution for Edit Distance problem In Java Description of Edit
* Distance with an Example:
*
* <p>Edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one
* another, by counting the minimum number of operations required to transform one string into the
* other. The distance operations are the removal, insertion, or substitution of a character in the
* string.
*
* <p>
* Edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to one another,
* by counting the minimum number of operations required to transform one string into the other. The
* distance operations are the removal, insertion, or substitution of a character in the string.
* <p>
* <p>
* The Distance between "kitten" and "sitting" is 3. A minimal edit script that transforms the former into the latter is:
* <p>
* kitten → sitten (substitution of "s" for "k")
* sitten → sittin (substitution of "i" for "e")
*
* <p>The Distance between "kitten" and "sitting" is 3. A minimal edit script that transforms the
* former into the latter is:
*
* <p>kitten → sitten (substitution of "s" for "k") sitten → sittin (substitution of "i" for "e")
* sittin → sitting (insertion of "g" at the end).
*
* @author SUBHAM SANGHAI
**/
*/
import java.util.Scanner;
public class EditDistance {
public static int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
// len1+1, len2+1, because finally return dp[len1][len2]
int[][] dp = new int[len1 + 1][len2 + 1];
/* If second string is empty, the only option is to
insert all characters of first string into second*/
for (int i = 0; i <= len1; i++) {
dp[i][0] = i;
}
/* If first string is empty, the only option is to
insert all characters of second string into first*/
for (int j = 0; j <= len2; j++) {
dp[0][j] = j;
}
//iterate though, and check last char
for (int i = 0; i < len1; i++) {
char c1 = word1.charAt(i);
for (int j = 0; j < len2; j++) {
char c2 = word2.charAt(j);
//if last two chars equal
if (c1 == c2) {
//update dp value for +1 length
dp[i + 1][j + 1] = dp[i][j];
} else {
/* if two characters are different ,
then take the minimum of the various operations(i.e insertion,removal,substitution)*/
int replace = dp[i][j] + 1;
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;
dp[i + 1][j + 1] = min;
}
}
}
/* return the final answer , after traversing through both the strings*/
return dp[len1][len2];
public static int minDistance(String word1, String word2) {
int len1 = word1.length();
int len2 = word2.length();
// len1+1, len2+1, because finally return dp[len1][len2]
int[][] dp = new int[len1 + 1][len2 + 1];
/* If second string is empty, the only option is to
insert all characters of first string into second*/
for (int i = 0; i <= len1; i++) {
dp[i][0] = i;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s1, s2;
System.out.println("Enter the First String");
s1 = input.nextLine();
System.out.println("Enter the Second String");
s2 = input.nextLine();
//ans stores the final Edit Distance between the two strings
int ans = minDistance(s1, s2);
System.out.println("The minimum Edit Distance between \"" + s1 + "\" and \"" + s2 + "\" is " + ans);
input.close();
/* If first string is empty, the only option is to
insert all characters of second string into first*/
for (int j = 0; j <= len2; j++) {
dp[0][j] = j;
}
// iterate though, and check last char
for (int i = 0; i < len1; i++) {
char c1 = word1.charAt(i);
for (int j = 0; j < len2; j++) {
char c2 = word2.charAt(j);
// if last two chars equal
if (c1 == c2) {
// update dp value for +1 length
dp[i + 1][j + 1] = dp[i][j];
} else {
/* if two characters are different ,
then take the minimum of the various operations(i.e insertion,removal,substitution)*/
int replace = dp[i][j] + 1;
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;
dp[i + 1][j + 1] = min;
}
}
}
/* return the final answer , after traversing through both the strings*/
return dp[len1][len2];
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s1, s2;
System.out.println("Enter the First String");
s1 = input.nextLine();
System.out.println("Enter the Second String");
s2 = input.nextLine();
// ans stores the final Edit Distance between the two strings
int ans = minDistance(s1, s2);
System.out.println(
"The minimum Edit Distance between \"" + s1 + "\" and \"" + s2 + "\" is " + ans);
input.close();
}
}

View File

@ -1,49 +1,45 @@
package DynamicProgramming;
/**
* DynamicProgramming solution for the Egg Dropping Puzzle
*/
/** DynamicProgramming solution for the Egg Dropping Puzzle */
public class EggDropping {
// min trials with n eggs and m floors
// min trials with n eggs and m floors
private static int minTrials(int n, int m) {
private static int minTrials(int n, int m) {
int[][] eggFloor = new int[n + 1][m + 1];
int result, x;
int[][] eggFloor = new int[n + 1][m + 1];
int result, x;
for (int i = 1; i <= n; i++) {
eggFloor[i][0] = 0; // Zero trial for zero floor.
eggFloor[i][1] = 1; // One trial for one floor
}
// j trials for only 1 egg
for (int j = 1; j <= m; j++)
eggFloor[1][j] = j;
// Using bottom-up approach in DP
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= m; j++) {
eggFloor[i][j] = Integer.MAX_VALUE;
for (x = 1; x <= j; x++) {
result = 1 + Math.max(eggFloor[i - 1][x - 1], eggFloor[i][j - x]);
// choose min of all values for particular x
if (result < eggFloor[i][j])
eggFloor[i][j] = result;
}
}
}
return eggFloor[n][m];
for (int i = 1; i <= n; i++) {
eggFloor[i][0] = 0; // Zero trial for zero floor.
eggFloor[i][1] = 1; // One trial for one floor
}
public static void main(String args[]) {
int n = 2, m = 4;
// result outputs min no. of trials in worst case for n eggs and m floors
int result = minTrials(n, m);
System.out.println(result);
// j trials for only 1 egg
for (int j = 1; j <= m; j++) eggFloor[1][j] = j;
// Using bottom-up approach in DP
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= m; j++) {
eggFloor[i][j] = Integer.MAX_VALUE;
for (x = 1; x <= j; x++) {
result = 1 + Math.max(eggFloor[i - 1][x - 1], eggFloor[i][j - x]);
// choose min of all values for particular x
if (result < eggFloor[i][j]) eggFloor[i][j] = result;
}
}
}
return eggFloor[n][m];
}
public static void main(String args[]) {
int n = 2, m = 4;
// result outputs min no. of trials in worst case for n eggs and m floors
int result = minTrials(n, m);
System.out.println(result);
}
}

View File

@ -4,97 +4,88 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* @author Varun Upadhyay (https://github.com/varunu28)
*/
/** @author Varun Upadhyay (https://github.com/varunu28) */
public class Fibonacci {
private static Map<Integer, Integer> map = new HashMap<>();
private static Map<Integer, Integer> map = new HashMap<>();
public static void main(String[] args) {
public static void main(String[] args) {
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
// Methods all returning [0, 1, 1, 2, 3, 5, ...] for n = [0, 1, 2, 3, 4, 5, ...]
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(fibMemo(n));
System.out.println(fibBotUp(n));
System.out.println(fibOptimized(n));
sc.close();
System.out.println(fibMemo(n));
System.out.println(fibBotUp(n));
System.out.println(fibOptimized(n));
sc.close();
}
/**
* This method finds the nth fibonacci number using memoization technique
*
* @param n The input n for which we have to determine the fibonacci number Outputs the nth
* fibonacci number
*/
public static int fibMemo(int n) {
if (map.containsKey(n)) {
return map.get(n);
}
/**
* This method finds the nth fibonacci number using memoization technique
*
* @param n The input n for which we have to determine the fibonacci number
* Outputs the nth fibonacci number
**/
public static int fibMemo(int n) {
if (map.containsKey(n)) {
return map.get(n);
}
int f;
int f;
if (n <= 1) {
f = n;
} else {
f = fibMemo(n - 1) + fibMemo(n - 2);
map.put(n, f);
}
return f;
}
if (n <= 1) {
f = n;
} else {
f = fibMemo(n - 1) + fibMemo(n - 2);
map.put(n, f);
}
return f;
/**
* This method finds the nth fibonacci number using bottom up
*
* @param n The input n for which we have to determine the fibonacci number Outputs the nth
* fibonacci number
*/
public static int fibBotUp(int n) {
Map<Integer, Integer> fib = new HashMap<>();
for (int i = 0; i <= n; i++) {
int f;
if (i <= 1) {
f = i;
} else {
f = fib.get(i - 1) + fib.get(i - 2);
}
fib.put(i, f);
}
/**
* This method finds the nth fibonacci number using bottom up
*
* @param n The input n for which we have to determine the fibonacci number
* Outputs the nth fibonacci number
**/
public static int fibBotUp(int n) {
return fib.get(n);
}
Map<Integer, Integer> fib = new HashMap<>();
for (int i = 0; i <= n; i++) {
int f;
if (i <= 1) {
f = i;
} else {
f = fib.get(i - 1) + fib.get(i - 2);
}
fib.put(i, f);
}
return fib.get(n);
/**
* This method finds the nth fibonacci number using bottom up
*
* @param n The input n for which we have to determine the fibonacci number Outputs the nth
* fibonacci number
* <p>This is optimized version of Fibonacci Program. Without using Hashmap and recursion. It
* saves both memory and time. Space Complexity will be O(1) Time Complexity will be O(n)
* <p>Whereas , the above functions will take O(n) Space.
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
*/
public static int fibOptimized(int n) {
if (n == 0) {
return 0;
}
/**
* This method finds the nth fibonacci number using bottom up
*
* @param n The input n for which we have to determine the fibonacci number
* Outputs the nth fibonacci number
* <p>
* This is optimized version of Fibonacci Program. Without using Hashmap and recursion.
* It saves both memory and time.
* Space Complexity will be O(1)
* Time Complexity will be O(n)
* <p>
* Whereas , the above functions will take O(n) Space.
* @author Shoaib Rayeen (https://github.com/shoaibrayeen)
**/
public static int fibOptimized(int n) {
if (n == 0) {
return 0;
}
int prev = 0, res = 1, next;
for (int i = 2; i <= n; i++) {
next = prev + res;
prev = res;
res = next;
}
return res;
int prev = 0, res = 1, next;
for (int i = 2; i <= n; i++) {
next = prev + res;
prev = res;
res = next;
}
return res;
}
}

View File

@ -5,68 +5,66 @@ import java.util.Queue;
import java.util.Vector;
public class FordFulkerson {
final static int INF = 987654321;
// edges
static int V;
static int[][] capacity, flow;
static final int INF = 987654321;
// edges
static int V;
static int[][] capacity, flow;
public static void main(String[] args) {
System.out.println("V : 6");
V = 6;
capacity = new int[V][V];
public static void main(String[] args) {
System.out.println("V : 6");
V = 6;
capacity = new int[V][V];
capacity[0][1] = 12;
capacity[0][3] = 13;
capacity[1][2] = 10;
capacity[2][3] = 13;
capacity[2][4] = 3;
capacity[2][5] = 15;
capacity[3][2] = 7;
capacity[3][4] = 15;
capacity[4][5] = 17;
capacity[0][1] = 12;
capacity[0][3] = 13;
capacity[1][2] = 10;
capacity[2][3] = 13;
capacity[2][4] = 3;
capacity[2][5] = 15;
capacity[3][2] = 7;
capacity[3][4] = 15;
capacity[4][5] = 17;
System.out.println("Max capacity in networkFlow : " + networkFlow(0, 5));
System.out.println("Max capacity in networkFlow : " + networkFlow(0, 5));
}
private static int networkFlow(int source, int sink) {
flow = new int[V][V];
int totalFlow = 0;
while (true) {
Vector<Integer> parent = new Vector<>(V);
for (int i = 0; i < V; i++) parent.add(-1);
Queue<Integer> q = new LinkedList<>();
parent.set(source, source);
q.add(source);
while (!q.isEmpty() && parent.get(sink) == -1) {
int here = q.peek();
q.poll();
for (int there = 0; there < V; ++there)
if (capacity[here][there] - flow[here][there] > 0 && parent.get(there) == -1) {
q.add(there);
parent.set(there, here);
}
}
if (parent.get(sink) == -1) break;
int amount = INF;
String printer = "path : ";
StringBuilder sb = new StringBuilder();
for (int p = sink; p != source; p = parent.get(p)) {
amount = Math.min(capacity[parent.get(p)][p] - flow[parent.get(p)][p], amount);
sb.append(p + "-");
}
sb.append(source);
for (int p = sink; p != source; p = parent.get(p)) {
flow[parent.get(p)][p] += amount;
flow[p][parent.get(p)] -= amount;
}
totalFlow += amount;
printer += sb.reverse() + " / max flow : " + totalFlow;
System.out.println(printer);
}
private static int networkFlow(int source, int sink) {
flow = new int[V][V];
int totalFlow = 0;
while (true) {
Vector<Integer> parent = new Vector<>(V);
for (int i = 0; i < V; i++)
parent.add(-1);
Queue<Integer> q = new LinkedList<>();
parent.set(source, source);
q.add(source);
while (!q.isEmpty() && parent.get(sink) == -1) {
int here = q.peek();
q.poll();
for (int there = 0; there < V; ++there)
if (capacity[here][there] - flow[here][there] > 0 && parent.get(there) == -1) {
q.add(there);
parent.set(there, here);
}
}
if (parent.get(sink) == -1)
break;
int amount = INF;
String printer = "path : ";
StringBuilder sb = new StringBuilder();
for (int p = sink; p != source; p = parent.get(p)) {
amount = Math.min(capacity[parent.get(p)][p] - flow[parent.get(p)][p], amount);
sb.append(p + "-");
}
sb.append(source);
for (int p = sink; p != source; p = parent.get(p)) {
flow[parent.get(p)][p] += amount;
flow[p][parent.get(p)] -= amount;
}
totalFlow += amount;
printer += sb.reverse() + " / max flow : " + totalFlow;
System.out.println(printer);
}
return totalFlow;
}
return totalFlow;
}
}

View File

@ -3,53 +3,50 @@ package DynamicProgramming;
import java.util.Scanner;
/**
* Program to implement Kadanes Algorithm to
* calculate maximum contiguous subarray sum of an array
* Program to implement Kadanes Algorithm to calculate maximum contiguous subarray sum of an array
* Time Complexity: O(n)
*
* @author Nishita Aggarwal
*/
public class KadaneAlgorithm {
/**
* This method implements Kadane's Algorithm
*
* @param arr The input array
* @return The maximum contiguous subarray sum of the array
*/
static int largestContiguousSum(int arr[]) {
int i, len = arr.length, cursum = 0, maxsum = Integer.MIN_VALUE;
if (len == 0) //empty array
return 0;
for (i = 0; i < len; i++) {
cursum += arr[i];
if (cursum > maxsum) {
maxsum = cursum;
}
if (cursum <= 0) {
cursum = 0;
}
}
return maxsum;
/**
* This method implements Kadane's Algorithm
*
* @param arr The input array
* @return The maximum contiguous subarray sum of the array
*/
static int largestContiguousSum(int arr[]) {
int i, len = arr.length, cursum = 0, maxsum = Integer.MIN_VALUE;
if (len == 0) // empty array
return 0;
for (i = 0; i < len; i++) {
cursum += arr[i];
if (cursum > maxsum) {
maxsum = cursum;
}
if (cursum <= 0) {
cursum = 0;
}
}
return maxsum;
}
/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, arr[], i;
n = sc.nextInt();
arr = new int[n];
for (i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int maxContSum = largestContiguousSum(arr);
System.out.println(maxContSum);
sc.close();
/**
* Main method
*
* @param args Command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, arr[], i;
n = sc.nextInt();
arr = new int[n];
for (i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int maxContSum = largestContiguousSum(arr);
System.out.println(maxContSum);
sc.close();
}
}

View File

@ -1,39 +1,32 @@
package DynamicProgramming;
/**
* A DynamicProgramming based solution for 0-1 Knapsack problem
*/
/** A DynamicProgramming based solution for 0-1 Knapsack problem */
public class Knapsack {
private static int knapSack(int W, int wt[], int val[], int n) throws IllegalArgumentException {
if(wt == null || val == null)
throw new IllegalArgumentException();
int i, w;
int rv[][] = new int[n + 1][W + 1]; //rv means return value
private static int knapSack(int W, int wt[], int val[], int n) throws IllegalArgumentException {
if (wt == null || val == null) throw new IllegalArgumentException();
int i, w;
int rv[][] = new int[n + 1][W + 1]; // rv means return value
// Build table rv[][] in bottom up manner
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0)
rv[i][w] = 0;
else if (wt[i - 1] <= w)
rv[i][w] = Math.max(val[i - 1] + rv[i - 1][w - wt[i - 1]], rv[i - 1][w]);
else
rv[i][w] = rv[i - 1][w];
}
}
return rv[n][W];
// Build table rv[][] in bottom up manner
for (i = 0; i <= n; i++) {
for (w = 0; w <= W; w++) {
if (i == 0 || w == 0) rv[i][w] = 0;
else if (wt[i - 1] <= w)
rv[i][w] = Math.max(val[i - 1] + rv[i - 1][w - wt[i - 1]], rv[i - 1][w]);
else rv[i][w] = rv[i - 1][w];
}
}
return rv[n][W];
}
// Driver program to test above function
public static void main(String args[]) {
int val[] = new int[]{50, 100, 130};
int wt[] = new int[]{10, 20, 40};
int W = 50;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
}
// Driver program to test above function
public static void main(String args[]) {
int val[] = new int[] {50, 100, 130};
int wt[] = new int[] {10, 20, 40};
int W = 50;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
}
}

View File

@ -1,56 +1,52 @@
package DynamicProgramming;
/**
* @author Kshitij VERMA (github.com/kv19971)
* LEVENSHTEIN DISTANCE dyamic programming implementation to show the difference between two strings (https://en.wikipedia.org/wiki/Levenshtein_distance)
* @author Kshitij VERMA (github.com/kv19971) LEVENSHTEIN DISTANCE dyamic programming implementation
* to show the difference between two strings
* (https://en.wikipedia.org/wiki/Levenshtein_distance)
*/
public class LevenshteinDistance {
private static int minimum(int a, int b, int c) {
if (a < b && a < c) {
return a;
} else if (b < a && b < c) {
return b;
private static int minimum(int a, int b, int c) {
if (a < b && a < c) {
return a;
} else if (b < a && b < c) {
return b;
} else {
return c;
}
}
private static int calculate_distance(String a, String b) {
int len_a = a.length() + 1;
int len_b = b.length() + 1;
int[][] distance_mat = new int[len_a][len_b];
for (int i = 0; i < len_a; i++) {
distance_mat[i][0] = i;
}
for (int j = 0; j < len_b; j++) {
distance_mat[0][j] = j;
}
for (int i = 0; i < len_a; i++) {
for (int j = 0; j < len_b; j++) {
int cost;
if (a.charAt(i) == b.charAt(j)) {
cost = 0;
} else {
return c;
cost = 1;
}
distance_mat[i][j] =
minimum(distance_mat[i - 1][j], distance_mat[i - 1][j - 1], distance_mat[i][j - 1])
+ cost;
}
}
return distance_mat[len_a - 1][len_b - 1];
}
private static int calculate_distance(String a, String b) {
int len_a = a.length() + 1;
int len_b = b.length() + 1;
int[][] distance_mat = new int[len_a][len_b];
for (int i = 0; i < len_a; i++) {
distance_mat[i][0] = i;
}
for (int j = 0; j < len_b; j++) {
distance_mat[0][j] = j;
}
for (int i = 0; i < len_a; i++) {
for (int j = 0; j < len_b; j++) {
int cost;
if (a.charAt(i) == b.charAt(j)) {
cost = 0;
} else {
cost = 1;
}
distance_mat[i][j] = minimum(distance_mat[i - 1][j], distance_mat[i - 1][j - 1], distance_mat[i][j - 1]) + cost;
public static void main(String[] args) {
String a = ""; // enter your string here
String b = ""; // enter your string here
}
}
return distance_mat[len_a - 1][len_b - 1];
}
public static void main(String[] args) {
String a = ""; // enter your string here
String b = ""; // enter your string here
System.out.print("Levenshtein distance between " + a + " and " + b + " is: ");
System.out.println(calculate_distance(a, b));
}
System.out.print("Levenshtein distance between " + a + " and " + b + " is: ");
System.out.println(calculate_distance(a, b));
}
}

View File

@ -2,67 +2,63 @@ package DynamicProgramming;
class LongestCommonSubsequence {
public static String getLCS(String str1, String str2) {
public static String getLCS(String str1, String str2) {
//At least one string is null
if (str1 == null || str2 == null)
return null;
// At least one string is null
if (str1 == null || str2 == null) return null;
//At least one string is empty
if (str1.length() == 0 || str2.length() == 0)
return "";
// At least one string is empty
if (str1.length() == 0 || str2.length() == 0) return "";
String[] arr1 = str1.split("");
String[] arr2 = str2.split("");
String[] arr1 = str1.split("");
String[] arr2 = str2.split("");
//lcsMatrix[i][j] = LCS of first i elements of arr1 and first j characters of arr2
int[][] lcsMatrix = new int[arr1.length + 1][arr2.length + 1];
// lcsMatrix[i][j] = LCS of first i elements of arr1 and first j characters of arr2
int[][] lcsMatrix = new int[arr1.length + 1][arr2.length + 1];
for (int i = 0; i < arr1.length + 1; i++)
lcsMatrix[i][0] = 0;
for (int j = 1; j < arr2.length + 1; j++)
lcsMatrix[0][j] = 0;
for (int i = 1; i < arr1.length + 1; i++) {
for (int j = 1; j < arr2.length + 1; j++) {
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];
}
}
for (int i = 0; i < arr1.length + 1; i++) lcsMatrix[i][0] = 0;
for (int j = 1; j < arr2.length + 1; j++) lcsMatrix[0][j] = 0;
for (int i = 1; i < arr1.length + 1; i++) {
for (int j = 1; j < arr2.length + 1; j++) {
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];
}
return lcsString(str1, str2, lcsMatrix);
}
}
return lcsString(str1, str2, lcsMatrix);
}
public static String lcsString(String str1, String str2, int[][] lcsMatrix) {
StringBuilder lcs = new StringBuilder();
int i = str1.length(),
j = str2.length();
while (i > 0 && j > 0) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
lcs.append(str1.charAt(i - 1));
i--;
j--;
} else if (lcsMatrix[i - 1][j] > lcsMatrix[i][j - 1]) {
i--;
} else {
j--;
}
}
return lcs.reverse().toString();
public static String lcsString(String str1, String str2, int[][] lcsMatrix) {
StringBuilder lcs = new StringBuilder();
int i = str1.length(), j = str2.length();
while (i > 0 && j > 0) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
lcs.append(str1.charAt(i - 1));
i--;
j--;
} else if (lcsMatrix[i - 1][j] > lcsMatrix[i][j - 1]) {
i--;
} else {
j--;
}
}
return lcs.reverse().toString();
}
public static void main(String[] args) {
String str1 = "DSGSHSRGSRHTRD";
String str2 = "DATRGAGTSHS";
String lcs = getLCS(str1, str2);
public static void main(String[] args) {
String str1 = "DSGSHSRGSRHTRD";
String str2 = "DATRGAGTSHS";
String lcs = getLCS(str1, str2);
//Print LCS
if (lcs != null) {
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
System.out.println("LCS: " + lcs);
System.out.println("LCS length: " + lcs.length());
}
// Print LCS
if (lcs != null) {
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2);
System.out.println("LCS: " + lcs);
System.out.println("LCS length: " + lcs.length());
}
}
}
}

View File

@ -2,64 +2,58 @@ package DynamicProgramming;
import java.util.Scanner;
/**
* @author Afrizal Fikri (https://github.com/icalF)
*/
/** @author Afrizal Fikri (https://github.com/icalF) */
public class LongestIncreasingSubsequence {
public static void main(String[] args) {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = sc.nextInt();
}
System.out.println(LIS(ar));
sc.close();
int ar[] = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = sc.nextInt();
}
private static int upperBound(int[] ar, int l, int r, int key) {
while (l < r - 1) {
int m = (l + r) >>> 1;
if (ar[m] >= key)
r = m;
else
l = m;
}
System.out.println(LIS(ar));
sc.close();
}
return r;
private static int upperBound(int[] ar, int l, int r, int key) {
while (l < r - 1) {
int m = (l + r) >>> 1;
if (ar[m] >= key) r = m;
else l = m;
}
private static int LIS(int[] array) {
int N = array.length;
if (N == 0)
return 0;
return r;
}
int[] tail = new int[N];
private static int LIS(int[] array) {
int N = array.length;
if (N == 0) return 0;
// always points empty slot in tail
int length = 1;
int[] tail = new int[N];
tail[0] = array[0];
for (int i = 1; i < N; i++) {
// always points empty slot in tail
int length = 1;
// new smallest value
if (array[i] < tail[0])
tail[0] = array[i];
tail[0] = array[0];
for (int i = 1; i < N; i++) {
// array[i] extends largest subsequence
else if (array[i] > tail[length - 1])
tail[length++] = array[i];
// new smallest value
if (array[i] < tail[0]) tail[0] = array[i];
// array[i] will become end candidate of an existing subsequence or
// Throw away larger elements in all LIS, to make room for upcoming grater elements than array[i]
// (and also, array[i] would have already appeared in one of LIS, identify the location and replace it)
else
tail[upperBound(tail, -1, length - 1, array[i])] = array[i];
}
// array[i] extends largest subsequence
else if (array[i] > tail[length - 1]) tail[length++] = array[i];
return length;
// array[i] will become end candidate of an existing subsequence or
// Throw away larger elements in all LIS, to make room for upcoming grater elements than
// array[i]
// (and also, array[i] would have already appeared in one of LIS, identify the location and
// replace it)
else tail[upperBound(tail, -1, length - 1, array[i])] = array[i];
}
}
return length;
}
}

View File

@ -1,57 +1,62 @@
package test;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.io.*;
import java.util.*;
/**
* Algorithm explanation https://www.educative.io/edpresso/longest-palindromic-subsequence-algorithm
*/
public class LongestPalindromicSubsequence {
public static void main(String[] args) {
String a = "BBABCBCAB";
String b = "BABCBAB";
public static void main(String[] args) {
String a = "BBABCBCAB";
String b = "BABCBAB";
String aLPS = LPS(a);
String bLPS = LPS(b);
String aLPS = LPS(a);
String bLPS = LPS(b);
System.out.println(a + " => " + aLPS);
System.out.println(b + " => " + bLPS);
}
System.out.println(a + " => " + aLPS);
System.out.println(b + " => " + bLPS);
}
public static String LPS(String original) throws IllegalArgumentException {
StringBuilder reverse = new StringBuilder(original);
reverse = reverse.reverse();
return recursiveLPS(original, reverse.toString());
}
public static String LPS(String original) throws IllegalArgumentException {
StringBuilder reverse = new StringBuilder(original);
reverse = reverse.reverse();
return recursiveLPS(original, reverse.toString());
}
private static String recursiveLPS(String original, String reverse) {
String bestResult = "";
//no more chars, then return empty
if(original.length() == 0 || reverse.length() == 0) {
bestResult = "";
private static String recursiveLPS(String original, String reverse) {
String bestResult = "";
// no more chars, then return empty
if (original.length() == 0 || reverse.length() == 0) {
bestResult = "";
} else {
// if the last chars match, then remove it from both strings and recur
if (original.charAt(original.length() - 1) == reverse.charAt(reverse.length() - 1)) {
String bestSubResult =
recursiveLPS(
original.substring(0, original.length() - 1),
reverse.substring(0, reverse.length() - 1));
bestResult = reverse.charAt(reverse.length() - 1) + bestSubResult;
} else {
// otherwise (1) ignore the last character of reverse, and recur on original and updated
// reverse again
// (2) ignore the last character of original and recur on the updated original and reverse
// again
// then select the best result from these two subproblems.
String bestSubResult1 = recursiveLPS(original, reverse.substring(0, reverse.length() - 1));
String bestSubResult2 = recursiveLPS(original.substring(0, original.length() - 1), reverse);
if (bestSubResult1.length() > bestSubResult2.length()) {
bestResult = bestSubResult1;
} else {
//if the last chars match, then remove it from both strings and recur
if(original.charAt(original.length() - 1) == reverse.charAt(reverse.length() - 1)) {
String bestSubResult = recursiveLPS(original.substring(0, original.length() - 1), reverse.substring(0, reverse.length() - 1));
bestResult = reverse.charAt(reverse.length() - 1) + bestSubResult;
} else {
//otherwise (1) ignore the last character of reverse, and recur on original and updated reverse again
//(2) ignore the last character of original and recur on the updated original and reverse again
//then select the best result from these two subproblems.
String bestSubResult1 = recursiveLPS(original, reverse.substring(0, reverse.length() - 1));
String bestSubResult2 = recursiveLPS(original.substring(0, original.length() - 1), reverse);
if(bestSubResult1.length()>bestSubResult2.length()) {
bestResult = bestSubResult1;
} else {
bestResult = bestSubResult2;
}
}
bestResult = bestSubResult2;
}
return bestResult;
}
}
}
return bestResult;
}
}

View File

@ -3,59 +3,56 @@ package DynamicProgramming;
import java.util.Scanner;
/**
* Given a string containing just the characters '(' and ')', find the length of
* the longest valid (well-formed) parentheses substring.
* Given a string containing just the characters '(' and ')', find the length of the longest valid
* (well-formed) parentheses substring.
*
* @author Libin Yang (https://github.com/yanglbme)
* @since 2018/10/5
*/
public class LongestValidParentheses {
public static int getLongestValidParentheses(String s) {
if (s == null || s.length() < 2) {
return 0;
public static int getLongestValidParentheses(String s) {
if (s == null || s.length() < 2) {
return 0;
}
char[] chars = s.toCharArray();
int n = chars.length;
int[] res = new int[n];
res[0] = 0;
res[1] = chars[1] == ')' && chars[0] == '(' ? 2 : 0;
int max = res[1];
for (int i = 2; i < n; ++i) {
if (chars[i] == ')') {
if (chars[i - 1] == '(') {
res[i] = res[i - 2] + 2;
} else {
int index = i - res[i - 1] - 1;
if (index >= 0 && chars[index] == '(') {
// ()(())
res[i] = res[i - 1] + 2 + (index - 1 >= 0 ? res[index - 1] : 0);
}
}
char[] chars = s.toCharArray();
int n = chars.length;
int[] res = new int[n];
res[0] = 0;
res[1] = chars[1] == ')' && chars[0] == '(' ? 2 : 0;
int max = res[1];
for (int i = 2; i < n; ++i) {
if (chars[i] == ')') {
if (chars[i - 1] == '(') {
res[i] = res[i - 2] + 2;
} else {
int index = i - res[i - 1] - 1;
if (index >= 0 && chars[index] == '(') {
// ()(())
res[i] = res[i - 1] + 2 + (index - 1 >= 0 ? res[index - 1] : 0);
}
}
}
max = Math.max(max, res[i]);
}
return max;
}
max = Math.max(max, res[i]);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
return max;
}
while (true) {
String str = sc.nextLine();
if ("quit".equals(str)) {
break;
}
int len = getLongestValidParentheses(str);
System.out.println(len);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
}
sc.close();
while (true) {
String str = sc.nextLine();
if ("quit".equals(str)) {
break;
}
int len = getLongestValidParentheses(str);
System.out.println(len);
}
sc.close();
}
}

View File

@ -5,132 +5,131 @@ import java.util.Arrays;
import java.util.Scanner;
public class MatrixChainMultiplication {
private static Scanner scan = new Scanner(System.in);
private static ArrayList<Matrix> mArray = new ArrayList<>();
private static int size;
private static int[][] m;
private static int[][] s;
private static int[] p;
private static Scanner scan = new Scanner(System.in);
private static ArrayList<Matrix> mArray = new ArrayList<>();
private static int size;
private static int[][] m;
private static int[][] s;
private static int[] p;
public static void main(String[] args) {
int count = 1;
while (true) {
String[] mSize = input("input size of matrix A(" + count + ") ( ex. 10 20 ) : ");
int col = Integer.parseInt(mSize[0]);
if (col == 0) break;
int row = Integer.parseInt(mSize[1]);
public static void main(String[] args) {
int count = 1;
while (true) {
String[] mSize = input("input size of matrix A(" + count + ") ( ex. 10 20 ) : ");
int col = Integer.parseInt(mSize[0]);
if (col == 0) break;
int row = Integer.parseInt(mSize[1]);
Matrix matrix = new Matrix(count, col, row);
mArray.add(matrix);
count++;
}
for (Matrix m : mArray) {
System.out.format("A(%d) = %2d x %2d%n", m.count(), m.col(), m.row());
}
size = mArray.size();
m = new int[size + 1][size + 1];
s = new int[size + 1][size + 1];
p = new int[size + 1];
for (int i = 0; i < size + 1; i++) {
Arrays.fill(m[i], -1);
Arrays.fill(s[i], -1);
}
for (int i = 0; i < p.length; i++) {
p[i] = i == 0 ? mArray.get(i).col() : mArray.get(i - 1).row();
}
matrixChainOrder();
for (int i = 0; i < size; i++) {
System.out.print("-------");
}
System.out.println();
printArray(m);
for (int i = 0; i < size; i++) {
System.out.print("-------");
}
System.out.println();
printArray(s);
for (int i = 0; i < size; i++) {
System.out.print("-------");
}
System.out.println();
System.out.println("Optimal solution : " + m[1][size]);
System.out.print("Optimal parens : ");
printOptimalParens(1, size);
Matrix matrix = new Matrix(count, col, row);
mArray.add(matrix);
count++;
}
for (Matrix m : mArray) {
System.out.format("A(%d) = %2d x %2d%n", m.count(), m.col(), m.row());
}
private static void printOptimalParens(int i, int j) {
if (i == j) {
System.out.print("A" + i);
} else {
System.out.print("(");
printOptimalParens(i, s[i][j]);
printOptimalParens(s[i][j] + 1, j);
System.out.print(")");
}
size = mArray.size();
m = new int[size + 1][size + 1];
s = new int[size + 1][size + 1];
p = new int[size + 1];
for (int i = 0; i < size + 1; i++) {
Arrays.fill(m[i], -1);
Arrays.fill(s[i], -1);
}
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.println();
}
for (int i = 0; i < p.length; i++) {
p[i] = i == 0 ? mArray.get(i).col() : mArray.get(i - 1).row();
}
private static void matrixChainOrder() {
for (int i = 1; i < size + 1; i++) {
m[i][i] = 0;
}
matrixChainOrder();
for (int i = 0; i < size; i++) {
System.out.print("-------");
}
System.out.println();
printArray(m);
for (int i = 0; i < size; i++) {
System.out.print("-------");
}
System.out.println();
printArray(s);
for (int i = 0; i < size; i++) {
System.out.print("-------");
}
System.out.println();
for (int l = 2; l < size + 1; l++) {
for (int i = 1; i < size - l + 2; i++) {
int j = i + l - 1;
m[i][j] = Integer.MAX_VALUE;
System.out.println("Optimal solution : " + m[1][size]);
System.out.print("Optimal parens : ");
printOptimalParens(1, size);
}
for (int k = i; k < j; k++) {
int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
private static void printOptimalParens(int i, int j) {
if (i == j) {
System.out.print("A" + i);
} else {
System.out.print("(");
printOptimalParens(i, s[i][j]);
printOptimalParens(s[i][j] + 1, j);
System.out.print(")");
}
}
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.println();
}
}
private static void matrixChainOrder() {
for (int i = 1; i < size + 1; i++) {
m[i][i] = 0;
}
private static String[] input(String string) {
System.out.print(string);
return (scan.nextLine().split(" "));
}
for (int l = 2; l < size + 1; l++) {
for (int i = 1; i < size - l + 2; i++) {
int j = i + l - 1;
m[i][j] = Integer.MAX_VALUE;
for (int k = i; k < j; k++) {
int q = m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j];
if (q < m[i][j]) {
m[i][j] = q;
s[i][j] = k;
}
}
}
}
}
private static String[] input(String string) {
System.out.print(string);
return (scan.nextLine().split(" "));
}
}
class Matrix {
private int count;
private int col;
private int row;
private int count;
private int col;
private int row;
Matrix(int count, int col, int row) {
this.count = count;
this.col = col;
this.row = row;
}
Matrix(int count, int col, int row) {
this.count = count;
this.col = col;
this.row = row;
}
int count() {
return count;
}
int count() {
return count;
}
int col() {
return col;
}
int col() {
return col;
}
int row() {
return row;
}
int row() {
return row;
}
}

View File

@ -1,12 +1,12 @@
package DynamicProgramming;
// Partition a set into two subsets such that the difference of subset sums is minimum
/*
Input: arr[] = {1, 6, 11, 5}
/*
Input: arr[] = {1, 6, 11, 5}
Output: 1
Explanation:
Subset1 = {1, 5, 6}, sum of Subset1 = 12
Subset2 = {11}, sum of Subset2 = 11
Subset1 = {1, 5, 6}, sum of Subset1 = 12
Subset2 = {11}, sum of Subset2 = 11
Input: arr[] = {36, 7, 46, 40}
Output: 23
@ -15,78 +15,75 @@ Subset1 = {7, 46} ; sum of Subset1 = 53
Subset2 = {36, 40} ; sum of Subset2 = 76
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class MinimumSumPartition
{
public static int subSet(int[] arr) {
int n = arr.length;
int sum = getSum(arr);
boolean[][] dp = new boolean[n + 1][sum + 1];
for (int i = 0; i <= n; i++) {
dp[i][0] = true;
}
for (int j = 0; j <= sum; j++) {
dp[0][j] = false;
}
import java.util.*;
//fill dp array
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (arr[i - 1] < j) {
dp[i][j] = dp[i - 1][j - arr[i - 1]] || dp[i - 1][j];
} else if (arr[i - 1] == j) {
dp[i][j] = true;
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
// fill the index array
int[] index = new int[sum];
int p = 0;
for (int i = 0; i <= sum / 2; i++) {
if (dp[n][i]) {
index[p++] = i;
}
}
return getMin(index, sum);
public class MinimumSumPartition {
public static int subSet(int[] arr) {
int n = arr.length;
int sum = getSum(arr);
boolean[][] dp = new boolean[n + 1][sum + 1];
for (int i = 0; i <= n; i++) {
dp[i][0] = true;
}
for (int j = 0; j <= sum; j++) {
dp[0][j] = false;
}
/**
* Calculate sum of array elements
*
* @param arr the array
* @return sum of given array
*/
public static int getSum(int[] arr) {
int sum = 0;
for (int temp : arr) {
sum += temp;
// fill dp array
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (arr[i - 1] < j) {
dp[i][j] = dp[i - 1][j - arr[i - 1]] || dp[i - 1][j];
} else if (arr[i - 1] == j) {
dp[i][j] = true;
} else {
dp[i][j] = dp[i - 1][j];
}
return sum;
}
}
public static int getMin(int[] arr, int sum) {
if (arr.length == 0) {
return 0;
}
int min = Integer.MAX_VALUE;
for (int temp : arr) {
min = Math.min(min, sum - 2 * temp);
}
return min;
// fill the index array
int[] index = new int[sum];
int p = 0;
for (int i = 0; i <= sum / 2; i++) {
if (dp[n][i]) {
index[p++] = i;
}
}
/**
* Driver Code
*/
public static void main(String[] args) {
assert subSet(new int[]{1, 6, 11,5}) == 1;
assert subSet(new int[]{36, 7, 46, 40}) == 23;
assert subSet(new int[]{1, 2, 3, 9}) == 3;
return getMin(index, sum);
}
/**
* Calculate sum of array elements
*
* @param arr the array
* @return sum of given array
*/
public static int getSum(int[] arr) {
int sum = 0;
for (int temp : arr) {
sum += temp;
}
return sum;
}
public static int getMin(int[] arr, int sum) {
if (arr.length == 0) {
return 0;
}
int min = Integer.MAX_VALUE;
for (int temp : arr) {
min = Math.min(min, sum - 2 * temp);
}
return min;
}
/** Driver Code */
public static void main(String[] args) {
assert subSet(new int[] {1, 6, 11, 5}) == 1;
assert subSet(new int[] {36, 7, 46, 40}) == 23;
assert subSet(new int[] {1, 2, 3, 9}) == 3;
}
}

View File

@ -1,33 +1,30 @@
package DynamicProgramming;
/**
* A DynamicProgramming solution for Rod cutting problem
* Returns the best obtainable price for a rod of
* length n and price[] as prices of different pieces
* A DynamicProgramming solution for Rod cutting problem Returns the best obtainable price for a rod
* of length n and price[] as prices of different pieces
*/
public class RodCutting {
private static int cutRod(int[] price, int n) {
int val[] = new int[n + 1];
val[0] = 0;
private static int cutRod(int[] price, int n) {
int val[] = new int[n + 1];
val[0] = 0;
for (int i = 1; i <= n; i++) {
int max_val = Integer.MIN_VALUE;
for (int j = 0; j < i; j++)
max_val = Math.max(max_val, price[j] + val[i - j - 1]);
for (int i = 1; i <= n; i++) {
int max_val = Integer.MIN_VALUE;
for (int j = 0; j < i; j++) max_val = Math.max(max_val, price[j] + val[i - j - 1]);
val[i] = max_val;
}
return val[n];
val[i] = max_val;
}
// main function to test
public static void main(String args[]) {
int[] arr = new int[]{2, 5, 13, 19, 20};
int size = arr.length;
int result = cutRod(arr,size);
System.out.println("Maximum Obtainable Value is " +
result);
}
return val[n];
}
// main function to test
public static void main(String args[]) {
int[] arr = new int[] {2, 5, 13, 19, 20};
int size = arr.length;
int result = cutRod(arr, size);
System.out.println("Maximum Obtainable Value is " + result);
}
}

View File

@ -2,45 +2,43 @@ package DynamicProgramming;
public class SubsetSum {
/**
* Driver Code
*/
public static void main(String[] args) {
int[] arr = new int[]{50, 4, 10, 15, 34};
assert subsetSum(arr, 64); /* 4 + 10 + 15 + 34 = 64 */
assert subsetSum(arr, 99); /* 50 + 15 + 34 = 99 */
assert !subsetSum(arr, 5);
assert !subsetSum(arr, 66);
/** Driver Code */
public static void main(String[] args) {
int[] arr = new int[] {50, 4, 10, 15, 34};
assert subsetSum(arr, 64); /* 4 + 10 + 15 + 34 = 64 */
assert subsetSum(arr, 99); /* 50 + 15 + 34 = 99 */
assert !subsetSum(arr, 5);
assert !subsetSum(arr, 66);
}
/**
* Test if a set of integers contains a subset that sum to a given integer.
*
* @param arr the array contains integers.
* @param sum target sum of subset.
* @return {@code true} if subset exists, otherwise {@code false}.
*/
private static boolean subsetSum(int[] arr, int sum) {
int n = arr.length;
boolean[][] isSum = new boolean[n + 2][sum + 1];
isSum[n + 1][0] = true;
for (int i = 1; i <= sum; i++) {
isSum[n + 1][i] = false;
}
/**
* Test if a set of integers contains a subset that sum to a given integer.
*
* @param arr the array contains integers.
* @param sum target sum of subset.
* @return {@code true} if subset exists, otherwise {@code false}.
*/
private static boolean subsetSum(int[] arr, int sum) {
int n = arr.length;
boolean[][] isSum = new boolean[n + 2][sum + 1];
isSum[n + 1][0] = true;
for (int i = 1; i <= sum; i++) {
isSum[n + 1][i] = false;
for (int i = n; i > 0; i--) {
isSum[i][0] = true;
for (int j = 1; j <= arr[i - 1] - 1; j++) {
if (j <= sum) {
isSum[i][j] = isSum[i + 1][j];
}
for (int i = n; i > 0; i--) {
isSum[i][0] = true;
for (int j = 1; j <= arr[i - 1] - 1; j++) {
if (j <= sum) {
isSum[i][j] = isSum[i + 1][j];
}
}
for (int j = arr[i - 1]; j <= sum; j++) {
isSum[i][j] = (isSum[i + 1][j] || isSum[i + 1][j - arr[i - 1]]);
}
}
return isSum[1][sum];
}
for (int j = arr[i - 1]; j <= sum; j++) {
isSum[i][j] = (isSum[i + 1][j] || isSum[i + 1][j - arr[i - 1]]);
}
}
}
return isSum[1][sum];
}
}