From cc6ce22409397f854d8928762646abb06f1c570f Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 14 May 2024 20:18:55 +0530 Subject: [PATCH] Added codes for 14 May --- GeeksForGeeks/May/14-5-24/GFG.java | 104 ++++++++++++++++++++++++++++ GeeksForGeeks/May/14-5-24/README.md | 2 + LeetCode/May/14-5-24/README.md | 2 + LeetCode/May/14-5-24/Solution.java | 29 ++++++++ 4 files changed, 137 insertions(+) create mode 100644 GeeksForGeeks/May/14-5-24/GFG.java create mode 100644 GeeksForGeeks/May/14-5-24/README.md create mode 100644 LeetCode/May/14-5-24/README.md create mode 100644 LeetCode/May/14-5-24/Solution.java diff --git a/GeeksForGeeks/May/14-5-24/GFG.java b/GeeksForGeeks/May/14-5-24/GFG.java new file mode 100644 index 0000000..34dddd4 --- /dev/null +++ b/GeeksForGeeks/May/14-5-24/GFG.java @@ -0,0 +1,104 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; + +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 rows; + rows = Integer.parseInt(br.readLine()); + + int columns; + columns = Integer.parseInt(br.readLine()); + + int[][] heights = IntMatrix.input(br, rows, columns); + + Solution obj = new Solution(); + int res = obj.MinimumEffort(rows, columns, heights); + + System.out.println(res); + } + } +} + +// } Driver Code Ends + + + +class Solution +{ + public static int MinimumEffort(int rows, int columns, int[][] heights) + { + // code here + int[][] effort = new int[rows][columns]; + for (int i = 0; i < rows; i++) + Arrays.fill(effort[i], Integer.MAX_VALUE); + + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[2] - b[2]); + pq.offer(new int[]{0, 0, 0}); // (row, col, effort) + + int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; + + while (!pq.isEmpty()) + { + int[] curr = pq.poll(); + int row = curr[0]; + int col = curr[1]; + int currEffort = curr[2]; + + if (row == rows - 1 && col == columns - 1) + return currEffort; + + for (int[] dir : directions) + { + int newRow = row + dir[0]; + int newCol = col + dir[1]; + + if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < columns) + { + int newEffort = Math.max(currEffort, Math.abs(heights[newRow][newCol] - heights[row][col])); + + if (newEffort < effort[newRow][newCol]) + { + effort[newRow][newCol] = newEffort; + pq.offer(new int[]{newRow, newCol, newEffort}); + } + } + } + } + + return -1; // Unreachable + } +} diff --git a/GeeksForGeeks/May/14-5-24/README.md b/GeeksForGeeks/May/14-5-24/README.md new file mode 100644 index 0000000..8faf6d9 --- /dev/null +++ b/GeeksForGeeks/May/14-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*m) +Space complexity - O(n*m) diff --git a/LeetCode/May/14-5-24/README.md b/LeetCode/May/14-5-24/README.md new file mode 100644 index 0000000..07b8c6a --- /dev/null +++ b/LeetCode/May/14-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(3^(n*m)) +Space complexity - O(n*m) diff --git a/LeetCode/May/14-5-24/Solution.java b/LeetCode/May/14-5-24/Solution.java new file mode 100644 index 0000000..14e77be --- /dev/null +++ b/LeetCode/May/14-5-24/Solution.java @@ -0,0 +1,29 @@ +class Solution +{ + public int getMaximumGold(int[][] grid) + { + int ans = 0; + + for (int i = 0; i < grid.length; ++i) + for (int j = 0; j < grid[0].length; ++j) + ans = Math.max(ans, dfs(grid, i, j)); + + return ans; + } + + private int dfs(int[][] grid, int i, int j) + { + if (i < 0 || j < 0 || i == grid.length || j == grid[0].length) + return 0; + if (grid[i][j] == 0) + return 0; + + int gold = grid[i][j]; + grid[i][j] = 0; // Mark as visited + int maxPath = Math.max(Math.max(dfs(grid, i + 1, j), dfs(grid, i - 1, j)), + Math.max(dfs(grid, i, j + 1), dfs(grid, i, j - 1))); + + grid[i][j] = gold; + return gold + maxPath; + } +}