Skip to content

Commit

Permalink
Added codes for 29 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 29, 2024
1 parent fc49db0 commit 8b7cf19
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
66 changes: 66 additions & 0 deletions GeeksForGeeks/January/29-1-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/January/29-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^3)
Space complexity - O(n^2)
2 changes: 2 additions & 0 deletions LeetCode/January/29-1-24/README.md
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)
30 changes: 30 additions & 0 deletions LeetCode/January/29-1-24/Solution.java
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<>();
}

0 comments on commit 8b7cf19

Please sign in to comment.