0% found this document useful (0 votes)
133 views8 pages

DP2

The document contains code for multiple dynamic programming problems including longest common subsequence (LCS), edit distance, knapsack, coin change, and maximum square submatrix with all zeros. It defines dynamic programming solutions to find the LCS of two strings, calculate the edit distance between two strings, solve the 0/1 knapsack problem, find the number of ways to make change for a given value, and find the size of the maximum square submatrix with all zeros in a binary matrix.

Uploaded by

Anubhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views8 pages

DP2

The document contains code for multiple dynamic programming problems including longest common subsequence (LCS), edit distance, knapsack, coin change, and maximum square submatrix with all zeros. It defines dynamic programming solutions to find the LCS of two strings, calculate the edit distance between two strings, solve the 0/1 knapsack problem, find the number of ways to make change for a given value, and find the size of the maximum square submatrix with all zeros in a binary matrix.

Uploaded by

Anubhav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

public class Solution {

public static String findWinner(int n, int x, int y) {


//We are assuming that y is greater than or equal to x
// if (x > y) {
// int temp = x;
// x = y;
// y = temp;
// }
boolean[] dp = new boolean[n + 1];

for (int i = 1; i <= n; i++) {

if (i == 1 || i == x || i == y) {
dp[i] = true;
}

else if (i < x) {
dp[i] = !dp[i - 1];
}

else if (i < y) {
dp[i] = !(dp[i - x] && dp[i - 1]);
}
else {
dp[i] = !(dp[i - x] && dp[i - y] && dp[i - 1]);
}
}

boolean result = dp[n];


if (result) {
return "Beerus";
} else {
return "Whis";
}
}

public class Solution {

public static int lcs(String s,String t,int i,int j,int dp[][]){


if(i==s.length() || j==t.length()){
return 0;
}
int myAns;
if(s.charAt(i)==t.charAt(j)){
int smallAns;
if(dp[i+1][j+1]==-1){
smallAns=lcs(s,t,i+1,j+1,dp);
dp[i+1][j+1]=smallAns;
}
else{
smallAns=dp[i+1][j+1];
}
myAns=1+smallAns;
}
else{
int firstAns,secondAns;
if(dp[i+1][j]==-1){
firstAns=lcs(s,t,i+1,j,dp);
dp[i+1][j]=firstAns;
}
else{
firstAns=dp[i+1][j];
}
if(dp[i][j+1]==-1){
secondAns=lcs(s,t,i,j+1,dp);
dp[i][j+1]=secondAns;
}
else{
secondAns=dp[i][j+1];
}
myAns=Math.max(firstAns,secondAns);
}
return myAns;
}

public static int lcs(String s, String t) {


int dp[][]=new int[s.length()+1][t.length()+1];
for(int i=0;i<dp.length;i++){
for(int j=0;j<dp[0].length;j++){
dp[i][j]=-1;
}
}
int myAns=lcs(s,t,0,0,dp);
return myAns;
//Your code goes here
}

import java.util.*;
public class Solution {

public static int minDis(String s1, String s2, int m, int n, int dp[][]){
if(m == 0)
return n;
if(n == 0)
return m;

// To check if the recursive tree


// for given n & m has already been executed
if(dp[m][n] != -1)
return dp[m][n];

// If characters are equal, execute


// recursive function for n-1, m-1
if(s1.charAt(m - 1) == s2.charAt(n - 1))
{
if(dp[m - 1][n - 1] == -1)
{
return dp[m][n] = minDis(s1, s2, m - 1, n - 1, dp);
}
else
return dp[m][n] = dp[m - 1][n - 1];
}

// If characters are nt equal, we need to

// find the minimum cost out of all 3 operations.


else
{
int m1, m2, m3; // temp variables
if(dp[m-1][n] != -1)
{
m1 = dp[m - 1][n];
}
else
{
m1 = minDis(s1, s2, m - 1, n, dp);
}

if(dp[m][n - 1] != -1)
{
m2 = dp[m][n - 1];
}
else
{
m2 = minDis(s1, s2, m, n - 1, dp);
}

if(dp[m - 1][n - 1] != -1)


{
m3 = dp[m - 1][n - 1];
}
else
{
m3 = minDis(s1, s2, m - 1, n - 1, dp);
}
return dp[m][n] = 1 + Math.min(m1, Math.min(m2, m3));
}
}

public static int editDistance(String s1, String s2){


int m=s1.length();
int n=s2.length();
int[][] dp = new int[m + 1][n + 1];
for(int i = 0; i < dp.length; i++){
for(int j = 0; j < dp[i].length ; j++){
dp[i][j]=-1;
}
}
int ans = minDis(s1,s2,m,n,dp);
return ans;
}
}
public static int knapSackRec(int W, int[] wt,int[] val, int n, int dp[][]){
// Base condition
if (n == 0 || W == 0)
return 0;

if (dp[n][W] != -1)
return dp[n][W];

if (wt[n - 1] > W)

// Store the value of function call


// stack in table before return
return dp[n][W] = knapSackRec(W, wt, val,
n - 1, dp);

else

// Return value of table after storing


return dp[n][W] = Math.max((val[n - 1] +
knapSackRec(W - wt[n - 1], wt,
val, n - 1, dp)),
knapSackRec(W, wt, val,
n - 1, dp));
}

public static int knapsack(int[] weights,int[] values, int n, int maxWeight){


// Declare the table dynamically
int dp[][] = new int[n + 1][maxWeight + 1];

// Loop to initially filled the


// table with -1
for(int i = 0; i < dp.length ; i++){
for(int j = 0; j < dp[0].length ; j++)
dp[i][j] = -1;
}

int ans = knapSackRec(maxWeight, weights, values , n , dp);


return ans;
}
}

public class Solution {

public static int countWaysToMakeChange(int denominations[], int value){


int ans=count(denominations,denominations.length,value);
return ans;
}

public static int count( int S[], int m, int n )


{
// table[i] will be storing the number of solutions for
// value i. We need n+1 rows as the table is constructed
// in bottom up manner using the base case (n = 0)
int table[]=new int[n+1];

// Base case (If given value is 0)


table[0] = 1;

// Pick all coins one by one and update the table[] values
// after the index greater than or equal to the value of the
// picked coin
for(int i=0; i<m; i++)
for(int j=S[i]; j<=n; j++)
table[j] += table[j-S[i]];

return table[n];
}
// Write your code here
}

public class Solution {

public static int findMaxSquareWithAllZeros(int[][] arr){


if(arr.length==0)return 0;
int m=arr.length;
int n= arr[0].length;
int dp[][]=new int[m][n];
for(int i=0;i<m;i++){
if(arr[i][0]==0){
dp[i][0]=1;
}
}
for(int j=0;j<n;j++){
if(arr[0][j]==0){
dp[0][j]=1;

}
}
int max=0;
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
if(arr[i][j]==0){
dp[i][j]=1+Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]));
if(max<dp[i][j]){
max=dp[i][j];
}
}
// else{
// dp[i][j]=1;
// }
}
}
return max;
}
}
public class Solution {

public static int findMaxSquareWithAllZeros(int[][] arr){


if(arr.length==0)return 0;
int m=arr.length;
int n= arr[0].length;
int dp[][]=new int[m][n];
for(int i=0;i<m;i++){
if(arr[i][0]==0){
dp[i][0]=1;
}
}
for(int j=0;j<n;j++){
if(arr[0][j]==0){
dp[0][j]=1;

}
}
int max=0;
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
if(arr[i][j]==0){
dp[i][j]=1+Math.min(dp[i-1][j-1],Math.min(dp[i-1][j],dp[i][j-1]));
if(max<dp[i][j]){
max=dp[i][j];
}
}
// else{
// dp[i][j]=1;
// }
}
}
return max;
}
}

public class Solution{

public static int getMinimumStrength(int[][] grid) {

/* Your class should be named Solution


* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int n = grid.length;
int m = grid[0].length;
int[][] dp = new int[n + 1][m + 1];
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
dp[i][j] = Integer.MAX_VALUE;
}
}
dp[n][m - 1] = 1;
dp[n - 1][m] = 1;
for (int i = n - 1; i >= 0; --i)
{
for (int j = m - 1; j >= 0; --j)
{
// 'neededStrength' stores the minimum strength needed to survive
int neededStrength = Integer.min(dp[i + 1][j], dp[i][j + 1]) -
grid[i][j];
dp[i][j] = (neededStrength <= 0) ? 1 : neededStrength;
}
}
int ans = dp[0][0];
return ans;
}
}

public class Solution {

public static int getMin(int arr[], int N){

/* Your class should be named Solution


* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int dp[] = new int[N+1];
dp[0] = 1;
int i = 0;
int sum = 0;
for(i = 1; i < N; i++){
if(arr[i] > arr[i-1]) {
dp[i] = dp[i-1]+1;
}
else dp[i] = 1;
}
for(i = N-2; i >= 0; i--){
if(arr[i] > arr[i+1] && dp[i] <= dp[i+1]){
dp[i] = dp[i+1]+1;
}
}
for(i = 0; i < N; i++)
sum += dp[i];
return sum;
}
}

public class Solution{


static boolean isSubsetPresent(int[] arr, int n, int sum) {

/* Your class should be named Solution


* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
boolean[][] dp = new boolean [n + 1][sum + 1];
for (int i = 0; i <= n; i++)
{
dp[i][0] = true;
}
for (int i = 1; i <= sum; i++)
{
dp[0][i] = false;
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= sum; j++)
{
if (j < arr[i - 1])
{
dp[i][j] = dp[i - 1][j];
}
else
{
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - arr[i - 1]];
}
}
}
boolean result = dp[n][sum];
return result;
}
}

You might also like