From 0b589d7b38eaa474555000c08504977007429997 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Thu, 14 Mar 2024 16:34:53 +0530 Subject: [PATCH] Added codes for 14 March --- GeeksForGeeks/March/14-3-24/GFG.java | 71 +++++++++++++++++++++++++++ GeeksForGeeks/March/14-3-24/README.md | 2 + LeetCode/March/14-3-24/README.md | 2 + LeetCode/March/14-3-24/Solution.java | 23 +++++++++ 4 files changed, 98 insertions(+) create mode 100644 GeeksForGeeks/March/14-3-24/GFG.java create mode 100644 GeeksForGeeks/March/14-3-24/README.md create mode 100644 LeetCode/March/14-3-24/README.md create mode 100644 LeetCode/March/14-3-24/Solution.java diff --git a/GeeksForGeeks/March/14-3-24/GFG.java b/GeeksForGeeks/March/14-3-24/GFG.java new file mode 100644 index 0000000..8269c87 --- /dev/null +++ b/GeeksForGeeks/March/14-3-24/GFG.java @@ -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; + } +}; diff --git a/GeeksForGeeks/March/14-3-24/README.md b/GeeksForGeeks/March/14-3-24/README.md new file mode 100644 index 0000000..b4189b7 --- /dev/null +++ b/GeeksForGeeks/March/14-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n^2) +Space complexity - O(n^2) diff --git a/LeetCode/March/14-3-24/README.md b/LeetCode/March/14-3-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/March/14-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/March/14-3-24/Solution.java b/LeetCode/March/14-3-24/Solution.java new file mode 100644 index 0000000..ae8062e --- /dev/null +++ b/LeetCode/March/14-3-24/Solution.java @@ -0,0 +1,23 @@ +class Solution +{ + public int numSubarraysWithSum(int[] nums, int goal) + { + int ans = 0; + int prefix = 0; + Map 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; + } +}