Skip to content

Commit

Permalink
Added codes for 26 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 26, 2024
1 parent e1b8be4 commit 35b2479
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
66 changes: 66 additions & 0 deletions GeeksForGeeks/April/26-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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 };
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/26-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*m)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/April/26-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*m)
Space complexity - O(n*m)
36 changes: 36 additions & 0 deletions LeetCode/April/26-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution
{
public int minFallingPathSum(int[][] grid)
{
int n = grid.length;

for (int i = 1; i < n; ++i)
{
Pair<Integer, Integer>[] 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<Integer, Integer>[] getTwoMinnumAndIndexes(int[] A)
{
List<Pair<Integer, Integer>> 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)};
}
}

0 comments on commit 35b2479

Please sign in to comment.