Skip to content

Commit

Permalink
Added codes for 26 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 26, 2024
1 parent 47bc047 commit 7fe0f7c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
50 changes: 50 additions & 0 deletions GeeksForGeeks/May/26-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//{ Driver Code Starts
// Initial Template for Java

import java.io.*;
import java.util.*;

class GfG {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String X = sc.next();
String Y = sc.next();
int costX = sc.nextInt();
int costY = sc.nextInt();

Solution ob = new Solution();
System.out.println(ob.findMinCost(X, Y, costX, costY));
}
}
}
// } Driver Code Ends


class Solution
{
public int findMinCost(String x, String y, int costX, int costY)
{
int n = x.length(), m = y.length();
int dp[][] = new int[n+1][m+1];

for (int i=0; i<=n; i++)
dp[i][0] = 0;

for (int i=0; i<=m; i++)
dp[0][i] = 0;

for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
if (x.charAt(i-1) == y.charAt(j-1))
dp[i][j] = 1 + dp[i-1][j-1];
else
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
}
}
return (n-dp[n][m])*costX + (m-dp[n][m])*costY;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/26-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*m)
Space complexity - O(n*m)
2 changes: 2 additions & 0 deletions LeetCode/May/26-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
36 changes: 36 additions & 0 deletions LeetCode/May/26-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution
{
public int checkRecord(int n)
{
int kMod = 1_000_000_007;
// dp[i][j] := the length so far with i A's and the last letters are j L's
long[][] dp = new long[2][3];
dp[0][0] = 1;

while (n-- > 0)
{
long[][] prev = Arrays.stream(dp).map((long[] A) -> A.clone())
.toArray((int length) -> new long[length][]);

// Append a P.
dp[0][0] = (prev[0][0] + prev[0][1] + prev[0][2]) % kMod;

// Append an L.
dp[0][1] = prev[0][0];

// Append an L.
dp[0][2] = prev[0][1];

// Append an A or append a P.
dp[1][0] = (prev[0][0] + prev[0][1] + prev[0][2] + prev[1][0] + prev[1][1] + prev[1][2]) % kMod;

// Append an L.
dp[1][1] = prev[1][0];

// Append an L.
dp[1][2] = prev[1][1];
}

return (int) ((dp[0][0] + dp[0][1] + dp[0][2] + dp[1][0] + dp[1][1] + dp[1][2]) % kMod);
}
}

0 comments on commit 7fe0f7c

Please sign in to comment.