-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1b8be4
commit 35b2479
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n*m) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n*m) | ||
Space complexity - O(n*m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}; | ||
} | ||
} |