-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3c0a3b
commit ea7c1a3
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
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
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]; | ||
} | ||
} |
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
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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n^2) | ||
Space complexity - O(1) |
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
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; | ||
} | ||
} |