This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class EditDistance { | |
public static int EditDistance(char[] a, char[] b) | |
{ | |
// a & b represent two strings. | |
// we need to make a DP table to store values | |
int [][] editDistanceMemo = new int [a.length + 1][b.length + 1]; | |
// Initialize all all elements in first row to i representing deletions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class LCS2 | |
{ | |
/* | |
* As usual for DP, three Steps are involved: | |
* 1. Break down the main problem into sub problems. | |
* 2. Initialize cache to hold solutions to sub problems. | |
* 3. Build solution to main problem from sub problem. | |
* Refer to this https://youtu.be/ASoaQq66foQ (Back To Back SWE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class PrimitiveCalculator | |
{ | |
/* | |
* As usual for DP, three Steps are involved: | |
* 1. Break down the main problem into sub problems. | |
* 2. Initialize cache to hold solutions to sub problems. | |
* 3. Build solution to main problem from sub problem. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class ChangeDP | |
{ | |
/* | |
* The idea is to break down the problem into smaller sub problem: | |
* Finding minimun change for m can be broken down into sub problems of finding minimum change for each input from | |
* 1 to m | |
* The bottom up approach | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.io.*; | |
class Inversions | |
{ | |
public static int mergeSort(int[] nums, int length) | |
{ | |
int inversionCount = 0; | |
// That is if the array is one single size return; | |
// This is the base case. | |
if(length < 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution | |
{ | |
public int[] sortArray(int[] nums) | |
{ | |
quickSort(nums, 0, nums.length - 1); | |
return nums; | |
} | |
public void quickSort(int[] nums, int left, int right) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution | |
{ | |
public int[] sortArray(int[] nums) | |
{ | |
mergeSort(nums, nums.length); | |
return nums; | |
} | |
public void mergeSort(int[] nums, int length) | |
{ |