# Conflicts:
#	Data Structures/HashMap/HashMap.java
#	Huffman.java
#	Misc/FloydTriangle.java
#	Misc/Huffman.java
#	Misc/InsertDeleteInArray.java
#	Misc/RootPrecision.java
#	Misc/ft.java
#	Misc/root_precision.java
#	Others/FloydTriangle.java
#	Others/Huffman.java
#	Others/insert_delete_in_array.java
#	Others/root_precision.java
#	insert_delete_in_array.java
This commit is contained in:
DESKTOP-0VAEMFL\joaom
2017-10-28 12:59:58 +01:00
58 changed files with 2697 additions and 114 deletions

View File

@ -0,0 +1,53 @@
//Dynamic Programming solution for the Egg Dropping Puzzle
public class EggDropping
{
// min trials with n eggs and m floors
private static int minTrials(int n, int m)
{
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];
}
//testing program
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

@ -11,7 +11,7 @@ import java.util.Map;
public class Fibonacci {
public static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
private static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
public static void main(String[] args) throws Exception {
@ -29,7 +29,7 @@ public class Fibonacci {
* Outputs the nth fibonacci number
**/
public static int fibMemo(int n) {
private static int fibMemo(int n) {
if (map.containsKey(n)) {
return map.get(n);
}
@ -54,7 +54,7 @@ public class Fibonacci {
* Outputs the nth fibonacci number
**/
public static int fibBotUp(int n) {
private static int fibBotUp(int n) {
Map<Integer,Integer> fib = new HashMap<Integer,Integer>();

View File

@ -7,7 +7,7 @@
*/
public class Levenshtein_distance{
private int minimum(int a, int b, int c){
private static int minimum(int a, int b, int c){
if(a < b && a < c){
return a;
}else if(b < a && b < c){
@ -16,9 +16,9 @@ public class Levenshtein_distance{
return c;
}
}
public int calculate_distance(String a, String b){
len_a = a.length() + 1;
len_b = b.length() + 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;

View File

@ -0,0 +1,66 @@
class LongestCommonSubsequence {
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 empty
if(str1.length() == 0 || str2.length() == 0)
return "";
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];
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);
}
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);
//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

@ -31,7 +31,7 @@ public class LongestIncreasingSubsequence {
return r;
}
public static int LIS(int[] array) {
private static int LIS(int[] array) {
int N = array.length;
if (N == 0)
return 0;

View File

@ -2,8 +2,7 @@
Returns the best obtainable price for a rod of
length n and price[] as prices of different pieces */
public class RodCutting
{
public class RodCutting {
private static int cutRod(int price[],int n)
{