Skip to content

Commit

Permalink
Added codes for 10 Feb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Feb 10, 2024
1 parent f3c0a3b commit ea7c1a3
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
72 changes: 72 additions & 0 deletions GeeksForGeeks/February/10-2-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//{ Driver Code Starts
//Initial Template for Java

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

class GFG
{
public static void main(String args[])throws IOException
{

BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while(t-- > 0)
{

int k = Integer.parseInt(read.readLine());
int n = Integer.parseInt(read.readLine());
int arr[][] = new int[n][n];
String input_line1[] = read.readLine().trim().split("\\s+");
int c = 0;
for(int i=0;i<n;i++){
for(int j = 0;j<n;j++){
arr[i][j] = Integer.parseInt(input_line1[c]);
c++;
}
}
Solution obj = new Solution();
System.out.println(obj.numberOfPath(n, k, arr));
}
}
}

// } Driver Code Ends


//User function Template for Java

class Solution
{
long numberOfPath(int n, int k, int [][]arr)
{
// code here
long dp[][][] = new long[n][n][k+1];

if(arr[n-1][n-1] <=k)
{
dp[n-1][n-1][arr[n-1][n-1]] = 1;
}

for(int i=n-1; i>=0 ; i--)
{
for(int j=n-1; j>=0 ; j--)
{
for(int m=1; m <= k ; m++)
{
if(m-arr[i][j] >= 0 && i+1<n)
{
dp[i][j][m] += dp[i+1][j][m-arr[i][j]];
}

if(m-arr[i][j] >= 0 && j+1<n)
{
dp[i][j][m] += dp[i][j+1][m-arr[i][j]];;
}
}
}
}

return dp[0][0][k];
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/February/10-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*m*k)
Space complexity - O(n*m*k)
2 changes: 2 additions & 0 deletions LeetCode/February/10-2-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(1)
29 changes: 29 additions & 0 deletions LeetCode/February/10-2-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution
{
public int countSubstrings(String s)
{
int ans = 0;

for (int i = 0; i < s.length(); ++i)
{
ans += extendPalindromes(s, i, i);
ans += extendPalindromes(s, i, i + 1);
}

return ans;
}

private int extendPalindromes(final String s, int l, int r)
{
int count = 0;

while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r))
{
++count;
--l;
++r;
}

return count;
}
}

0 comments on commit ea7c1a3

Please sign in to comment.