-
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
f2e9412
commit 0b589d7
Showing
4 changed files
with
98 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,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; | ||
} | ||
}; |
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(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(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,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; | ||
} | ||
} |