Skip to content

Commit

Permalink
Added codes for 13 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 13, 2024
1 parent 76ad07f commit d80a672
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
106 changes: 106 additions & 0 deletions GeeksForGeeks/May/13-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class IntMatrix {
public static int[][] input(BufferedReader br, int n, int m) throws IOException {
int[][] mat = new int[n][];

for (int i = 0; i < n; i++) {
String[] s = br.readLine().trim().split(" ");
mat[i] = new int[s.length];
for (int j = 0; j < s.length; j++) mat[i][j] = Integer.parseInt(s[j]);
}

return mat;
}

public static void print(int[][] m) {
for (var a : m) {
for (int e : a) System.out.print(e + " ");
System.out.println();
}
}

public static void print(ArrayList<ArrayList<Integer>> m) {
for (var a : m) {
for (int e : a) System.out.print(e + " ");
System.out.println();
}
}
}

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while (t-- > 0) {

int e;
e = Integer.parseInt(br.readLine());

int v;
v = Integer.parseInt(br.readLine());

int[][] edges = IntMatrix.input(br, e, 2);

Solution obj = new Solution();
int res = obj.findNumberOfGoodComponent(e, v, edges);

System.out.println(res);
}
}
}

// } Driver Code Ends



class Solution {
public static int findNumberOfGoodComponent(int e, int v, int[][] edges) {
// code here
ArrayList<ArrayList<Integer>> adj = new ArrayList<>(v + 1);
for (int i = 0; i <= v; i++)
adj.add(new ArrayList<Integer>());

for (int[] edge : edges)
{
adj.get(edge[0]).add(edge[1]);
adj.get(edge[1]).add(edge[0]);
}

int ans = 0;
int[] visited = new int[v + 1];

for (int i = 1; i <= v; i++)
{
if (visited[i] == 0)
{
int[] vertices = { 0 };
int[] edgeCount = { 0 };

dfs(i, vertices, edgeCount, adj, visited);
edgeCount[0] /= 2;

if (edgeCount[0] == (vertices[0] * (vertices[0] - 1)) / 2)
ans++;
}

}

return ans;
}


private static void dfs(int v, int[] vertices, int[] edges, ArrayList<ArrayList<Integer>> adj, int[] visited)
{
visited[v] = 1;
vertices[0]++;
edges[0] += adj.get(v).size();

for (int to : adj.get(v))
if (visited[to] == 0)
dfs(to, vertices, edges, adj, visited);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/13-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(v+e)
Space complexity - O(depth)
2 changes: 2 additions & 0 deletions LeetCode/May/13-5-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)
19 changes: 19 additions & 0 deletions LeetCode/May/13-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public int matrixScore(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int ans = m; // All the cells in the first column are 1.

for (int j = 1; j < n; ++j)
{
int onesCount = 0;
for (int i = 0; i < m; ++i)
// The best strategy is flipping the rows with a leading 0..
onesCount += grid[i][j] == grid[i][0] ? 1 : 0;

ans = ans * 2 + Math.max(onesCount, m - onesCount);
}

return ans;
}
}

0 comments on commit d80a672

Please sign in to comment.