From 35b24797a0d981db6abe3840313aeba2395828fd Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Fri, 26 Apr 2024 07:47:28 +0530 Subject: [PATCH] Added codes for 26 April --- GeeksForGeeks/April/26-4-24/GFG.java | 66 +++++++++++++++++++++++++++ GeeksForGeeks/April/26-4-24/README.md | 2 + LeetCode/April/26-4-24/README.md | 2 + LeetCode/April/26-4-24/Solution.java | 36 +++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 GeeksForGeeks/April/26-4-24/GFG.java create mode 100644 GeeksForGeeks/April/26-4-24/README.md create mode 100644 LeetCode/April/26-4-24/README.md create mode 100644 LeetCode/April/26-4-24/Solution.java diff --git a/GeeksForGeeks/April/26-4-24/GFG.java b/GeeksForGeeks/April/26-4-24/GFG.java new file mode 100644 index 0000000..1c0ce16 --- /dev/null +++ b/GeeksForGeeks/April/26-4-24/GFG.java @@ -0,0 +1,66 @@ +//{ Driver Code Starts +// Initial Template for Java + +import java.io.*; +import java.lang.*; +import java.util.*; + +class GFG { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine().trim()); + while (T-- > 0) { + String[] s = br.readLine().trim().split(" "); + int n = Integer.parseInt(s[0]); + int m = Integer.parseInt(s[1]); + int[][] matrix = new int[n][m]; + for (int i = 0; i < n; i++) { + String[] S = br.readLine().split(" "); + for (int j = 0; j < m; j++) { + matrix[i][j] = Integer.parseInt(S[j]); + } + } + Solution ob = new Solution(); + int[] ans = ob.FindExitPoint(n, m, matrix); + for (int i = 0; i < ans.length; i++) System.out.print(ans[i] + " "); + System.out.println(); + } + } +} + +// } Driver Code Ends + + +// User function Template for Java + +class Solution +{ + public int[] FindExitPoint(int n, int m, int[][] matrix) + { + // code here + int i = 0, j = 0, currentDirection = 0; + int[][] directions = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} }; + + while(i >= 0 && i < n && j >= 0 && j < m) + { + if(matrix[i][j] == 0) + { + i += directions[currentDirection][0]; + j += directions[currentDirection][1]; + } + else + { + matrix[i][j] = 0; + currentDirection++; + currentDirection %= 4; + i += directions[currentDirection][0]; + j += directions[currentDirection][1]; + } + } + + i -= directions[currentDirection][0]; + j -= directions[currentDirection][1]; + + return new int[] { i, j }; + } +} diff --git a/GeeksForGeeks/April/26-4-24/README.md b/GeeksForGeeks/April/26-4-24/README.md new file mode 100644 index 0000000..f988889 --- /dev/null +++ b/GeeksForGeeks/April/26-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*m) +Space complexity - O(1) diff --git a/LeetCode/April/26-4-24/README.md b/LeetCode/April/26-4-24/README.md new file mode 100644 index 0000000..8faf6d9 --- /dev/null +++ b/LeetCode/April/26-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*m) +Space complexity - O(n*m) diff --git a/LeetCode/April/26-4-24/Solution.java b/LeetCode/April/26-4-24/Solution.java new file mode 100644 index 0000000..a3517a9 --- /dev/null +++ b/LeetCode/April/26-4-24/Solution.java @@ -0,0 +1,36 @@ +class Solution +{ + public int minFallingPathSum(int[][] grid) + { + int n = grid.length; + + for (int i = 1; i < n; ++i) + { + Pair[] twoMinnumAndIndexes = getTwoMinnumAndIndexes(grid[i - 1]); + int firstMinNum = twoMinnumAndIndexes[0].getKey(); + int firstMinIndex = twoMinnumAndIndexes[0].getValue(); + int secondMinNum = twoMinnumAndIndexes[1].getKey(); + + for (int j = 0; j < n; ++j) + { + if (j == firstMinIndex) + grid[i][j] += secondMinNum; + else + grid[i][j] += firstMinNum; + } + } + + return Arrays.stream(grid[n - 1]).min().getAsInt(); + } + + private Pair[] getTwoMinnumAndIndexes(int[] A) + { + List> numAndIndexes = new ArrayList<>(); + + for (int i = 0; i < A.length; ++i) + numAndIndexes.add(new Pair<>(A[i], i)); + + Collections.sort(numAndIndexes, (a, b) -> a.getKey() - b.getKey()); + return new Pair[] {numAndIndexes.get(0), numAndIndexes.get(1)}; + } +}