-
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
e7a2c2d
commit 72b0660
Showing
4 changed files
with
121 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,61 @@ | ||
//{ Driver Code Starts | ||
// Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG { | ||
public static void main(String args[]) throws IOException { | ||
BufferedReader read = | ||
new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(read.readLine()); | ||
while (t-- > 0) { | ||
String s[] = read.readLine().split(" "); | ||
int N = Integer.parseInt(s[0]); | ||
int M = Integer.parseInt(s[1]); | ||
int A[][] = new int[N][M]; | ||
for (int i = 0; i < N; i++) { | ||
String S[] = read.readLine().split(" "); | ||
for (int j = 0; j < M; j++) { | ||
A[i][j] = Integer.parseInt(S[j]); | ||
} | ||
} | ||
Solution ob = new Solution(); | ||
System.out.println(ob.minRow(N, M, A)); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
// User function Template for Java | ||
|
||
class Solution | ||
{ | ||
int minRow(int n, int m, int a[][]) | ||
{ | ||
// code here | ||
int res=1; | ||
int min = Integer.MAX_VALUE; | ||
for(int i=0;i<a.length;i++) | ||
{ | ||
int count=0; | ||
for(int j=0;j<a[0].length;j++) | ||
{ | ||
if(a[i][j]==1) | ||
{ | ||
count++; | ||
} | ||
} | ||
|
||
int minn= Math.min(count, min); | ||
if(minn!=min) | ||
{ | ||
res=i+1; | ||
min=minn; | ||
} | ||
} | ||
return res; | ||
} | ||
}; |
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(10^4) | ||
Space complexity - O(10^4) |
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,56 @@ | ||
class Solution | ||
{ | ||
public int openLock(String[] deadends, String target) | ||
{ | ||
Set<String> seen = new HashSet<>(Arrays.asList(deadends)); | ||
if (seen.contains("0000")) | ||
return -1; | ||
if (target.equals("0000")) | ||
return 0; | ||
|
||
int ans = 0; | ||
Queue<String> q = new ArrayDeque<>(Arrays.asList("0000")); | ||
|
||
while (!q.isEmpty()) | ||
{ | ||
++ans; | ||
for (int sz = q.size(); sz > 0; --sz) | ||
{ | ||
StringBuilder sb = new StringBuilder(q.poll()); | ||
for (int i = 0; i < 4; ++i) | ||
{ | ||
char cache = sb.charAt(i); | ||
// Increase the i-th digit by 1. | ||
sb.setCharAt(i, sb.charAt(i) == '9' ? '0' : (char) (sb.charAt(i) + 1)); | ||
String word = sb.toString(); | ||
|
||
if (word.equals(target)) | ||
return ans; | ||
|
||
if (!seen.contains(word)) | ||
{ | ||
q.offer(word); | ||
seen.add(word); | ||
} | ||
|
||
sb.setCharAt(i, cache); | ||
// Decrease the i-th digit by 1. | ||
sb.setCharAt(i, sb.charAt(i) == '0' ? '9' : (char) (sb.charAt(i) - 1)); | ||
word = sb.toString(); | ||
|
||
if (word.equals(target)) | ||
return ans; | ||
|
||
if (!seen.contains(word)) | ||
{ | ||
q.offer(word); | ||
seen.add(word); | ||
} | ||
sb.setCharAt(i, cache); | ||
} | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} |