Skip to content

Commit

Permalink
Added codes for 22 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 22, 2024
1 parent e7a2c2d commit 72b0660
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
61 changes: 61 additions & 0 deletions GeeksForGeeks/April/22-4-24/GFG.java
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;
}
};
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/22-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/22-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(10^4)
Space complexity - O(10^4)
56 changes: 56 additions & 0 deletions LeetCode/April/22-4-24/Solution.java
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;
}
}

0 comments on commit 72b0660

Please sign in to comment.