-
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
fc49db0
commit 8b7cf19
Showing
4 changed files
with
100 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,66 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.util.*; | ||
import java.lang.*; | ||
import java.io.*; | ||
class GFG | ||
{ | ||
public static void main(String[] args) throws IOException | ||
{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int T = Integer.parseInt(br.readLine().trim()); | ||
while(T-->0) | ||
{ | ||
String str = br.readLine().trim(); | ||
Solution ob = new Solution(); | ||
int ans = ob.TotalCount(str); | ||
System.out.println(ans); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
public int TotalCount(String str) | ||
{ | ||
// Code here | ||
int i,j,sum=0,ans =1,n = str.length(); | ||
int[][] sums = new int[n][n]; | ||
|
||
for(i=0;i<n;i++) | ||
{ | ||
for(j=i;j<n;j++) | ||
{ | ||
if(i==j) | ||
sums[i][j] = str.charAt(j)-'0'; | ||
else | ||
sums[i][j] = sums[i][j-1]+ (str.charAt(j)-'0'); | ||
} | ||
} | ||
|
||
int[][] dp = new int[n][n]; | ||
|
||
Arrays.fill(dp[0],1); | ||
for(i=1;i<n;i++) | ||
{ | ||
for(j=i;j<n;j++) | ||
{ | ||
for(int k=0;k<=i-1;k++) | ||
{ | ||
if(sums[k][i-1] <= sums[i][j]) | ||
dp[i][j] += dp[k][i-1]; | ||
} | ||
|
||
} | ||
ans += dp[i][n-1]; | ||
} | ||
|
||
return ans; | ||
} | ||
} |
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^3) | ||
Space complexity - O(n^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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(1) and O(n) | ||
Space complexity - O(n) |
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,30 @@ | ||
class MyQueue | ||
{ | ||
public void push(int x) | ||
{ | ||
input.push(x); | ||
} | ||
|
||
public int pop() | ||
{ | ||
peek(); | ||
return output.pop(); | ||
} | ||
|
||
public int peek() | ||
{ | ||
if (output.isEmpty()) | ||
while (!input.isEmpty()) | ||
output.push(input.pop()); | ||
|
||
return output.peek(); | ||
} | ||
|
||
public boolean empty() | ||
{ | ||
return input.isEmpty() && output.isEmpty(); | ||
} | ||
|
||
private Deque<Integer> input = new ArrayDeque<>(); | ||
private Deque<Integer> output = new ArrayDeque<>(); | ||
} |