From cb9529dee3b21b990573406cc7a353c9600a6464 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Wed, 27 Mar 2024 16:57:57 +0530 Subject: [PATCH] Added codes for 27 March --- GeeksForGeeks/March/27-3-24/GFG.java | 137 ++++++++++++++++++++++++++ GeeksForGeeks/March/27-3-24/README.md | 2 + LeetCode/March/27-3-24/README.md | 2 + LeetCode/March/27-3-24/Solution.java | 23 +++++ 4 files changed, 164 insertions(+) create mode 100644 GeeksForGeeks/March/27-3-24/GFG.java create mode 100644 GeeksForGeeks/March/27-3-24/README.md create mode 100644 LeetCode/March/27-3-24/README.md create mode 100644 LeetCode/March/27-3-24/Solution.java diff --git a/GeeksForGeeks/March/27-3-24/GFG.java b/GeeksForGeeks/March/27-3-24/GFG.java new file mode 100644 index 0000000..229c7db --- /dev/null +++ b/GeeksForGeeks/March/27-3-24/GFG.java @@ -0,0 +1,137 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; + +class IntArray { + public static int[] input(BufferedReader br, int n) throws IOException { + String[] s = br.readLine().trim().split(" "); + int[] a = new int[n]; + for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]); + + return a; + } + + public static void print(int[] a) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + + public static void print(ArrayList a) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } +} + +class IntMatrix { + public static int[][] input(BufferedReader br, int n, int m) throws IOException { + int[][] mat = new int[n][]; + + for (int i = 0; i < n; i++) { + String[] s = br.readLine().trim().split(" "); + mat[i] = new int[s.length]; + for (int j = 0; j < s.length; j++) mat[i][j] = Integer.parseInt(s[j]); + } + + return mat; + } + + public static void print(int[][] m) { + for (var a : m) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + } + + public static void print(ArrayList> m) { + for (var a : m) { + for (int e : a) System.out.print(e + " "); + System.out.println(); + } + } +} + +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int t; + t = Integer.parseInt(br.readLine()); + while (t-- > 0) { + + int[] a = IntArray.input(br, 2); + + int[][] mat = IntMatrix.input(br, a[0], a[1]); + + Solution obj = new Solution(); + int res = obj.findShortestPath(mat); + + System.out.println(res); + } + } +} + +// } Driver Code Ends + + +class Solution +{ + public static int solve(int row, int col, int i, int j, int[][] mat, int[][] visited, int cnt) + { + if(i==row || i<0 || j==col || j<0 || mat[i][j]==-1 || visited[i][j]==1) + return Integer.MAX_VALUE; + + if(j == col-1) + { + return cnt; + } + + visited[i][j] = 1; + int l1 = solve(row, col, i+1, j, mat, visited, cnt+1); + int l2 = solve(row, col, i-1, j, mat, visited, cnt+1); + int l3 = solve(row, col, i, j+1, mat, visited, cnt+1); + int l4 = solve(row, col, i,j-1, mat, visited, cnt+1); + + visited[i][j] = 0; + + return Math.min(l1,Math.min(l2, Math.min(l3,l4))); + } + + public static int findShortestPath(int[][] mat) + { + // code here + int row = mat.length; + int col = mat[0].length; + int min = Integer.MAX_VALUE; + int[][] visited = new int[row][col]; + + for(int i = 0; i < row; i++ ) + { + for(int j = 0; j < col; j++) + { + if(mat[i][j]==0) + { + if(i-1>=0) + mat[i-1][j] = -1; + + if(i+1 < row) + mat[i+1][j] = -1; + + if(j-1 >= 0) + mat[i][j-1] = -1; + + if(j+1 < col) + mat[i][j+1] = -1; + + + } + } + } + + for(int i = 0; i < row; i++) + { + if(mat[i][0] == 1) + min = Math.min(min, solve(row, col, i, 0, mat, visited,0)); + } + + return min == Integer.MAX_VALUE? -1: min+1; + } +} diff --git a/GeeksForGeeks/March/27-3-24/README.md b/GeeksForGeeks/March/27-3-24/README.md new file mode 100644 index 0000000..fd4385d --- /dev/null +++ b/GeeksForGeeks/March/27-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(r*c) +Space complexity - O(r*c) diff --git a/LeetCode/March/27-3-24/README.md b/LeetCode/March/27-3-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/March/27-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/March/27-3-24/Solution.java b/LeetCode/March/27-3-24/Solution.java new file mode 100644 index 0000000..4a3d0d7 --- /dev/null +++ b/LeetCode/March/27-3-24/Solution.java @@ -0,0 +1,23 @@ +class Solution +{ + public int numSubarrayProductLessThanK(int[] nums, int k) + { + if (k <= 1) + return 0; + + int ans = 0; + int prod = 1; + + for (int l = 0, r = 0; r < nums.length; ++r) + { + prod *= nums[r]; + + while (prod >= k) + prod /= nums[l++]; + + ans += r - l + 1; + } + + return ans; + } +}