Skip to content

Commit

Permalink
Added codes for 14 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 14, 2024
1 parent f2e9412 commit 0b589d7
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
71 changes: 71 additions & 0 deletions GeeksForGeeks/March/14-3-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//{ 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 N = Integer.parseInt(read.readLine());
char A[][] = new char[N][N];
for (int i = 0; i < N; i++) {
String S[] = read.readLine().trim().split(" ");
for (int j = 0; j < N; j++) A[i][j] = S[j].charAt(0);
}
Solution ob = new Solution();
System.out.println(ob.largestSubsquare(N, A));
}
}
}
// } Driver Code Ends


// User function Template for Java

class Solution
{
int largestSubsquare(int n, char a[][])
{
// code here
int side=0;
int[][] ver = new int[n][n];
int[][] hor = new int[n][n];

for(int i = 0; i < n ; i++)
{
for(int j = 0 ; j < n ; j++)
{
if(a[i][j] == 'X')
{
ver[i][j] = ((i == 0) ? 1 : ver[i-1][j] + 1);
hor[i][j] = ((j == 0) ? 1 : hor[i][j-1] + 1);
}
}
}


for(int i = n-1 ; i >= 0 ; i--)
{
for(int j = n-1 ; j >= 0 ; j--)
{
int val = Math.min(ver[i][j] , hor[i][j]);

while(val > side)
{
if(ver[i][j-val+1] >= val && hor[i-val+1][j] >= val)
{
side = val;
}

val--;
}
}
}

return side;
}
};
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/14-3-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(n^2)
2 changes: 2 additions & 0 deletions LeetCode/March/14-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
23 changes: 23 additions & 0 deletions LeetCode/March/14-3-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution
{
public int numSubarraysWithSum(int[] nums, int goal)
{
int ans = 0;
int prefix = 0;
Map<Integer, Integer> count = new HashMap<>();
count.put(0, 1);

for (int num : nums)
{
prefix += num;
int key = prefix - goal;

if (count.containsKey(key))
ans += count.get(key);

count.merge(prefix, 1, Integer::sum);
}

return ans;
}
}

0 comments on commit 0b589d7

Please sign in to comment.